Design Patterns in Android with Kotlin - GeeksforGeeks

10 Nov, 2021
Design patterns is basically a solution or blueprint for a problem that we get over and over again in programming, so they are just typical types of problems we can encounter as programmers, and these design patterns are just a good way to solve those problems, there is a lot of design pattern in android. So basically, they are three categories as below:
Creational patterns: How you create objects.Structural patterns: How you compose objects.Behavioral patterns: How you coordinate object interactions.
Creational patterns: How you create objects.
Structural patterns: How you compose objects.
Behavioral patterns: How you coordinate object interactions.
Now we are going to discuss the most important design patterns that you should know.
These are the design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation. These are the design patterns are come under this category.
Singleton
Builder
Dependency Injection
Factory
Let’s quickly talk about them:
Singleton:
The singleton pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance. There are very few applications, do not overuse this pattern.
It’s very easy to accomplish in Kotlin but not in java. because in java there is no native implementation of the singleton.
Kotlin
object eSingleton {  fun doing() {    // ...  }}
for accessing member of singleton object you can call like this :
eSingleton.doing()
By using the object, you’ll know you’re using the same instance of that class throughout your app.
Builder:
The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm. An external class controls the construction algorithm. Click here
Factory:
As the name suggests, Factory takes care of all the object creational logic. In this pattern, a factory class controls which object to instantiate. Factory pattern comes in handy when dealing with many common objects. You can use it where you might not want to specify a concrete class. Click here
Dependency Injection:
Dependency injection is like moving into a furnished apartment. Everything you need is already there. You don’t have to wait for furniture delivery or follow pages of IKEA instructions to put together a Borgsjö bookshelf.
In software terms, dependency injection has you provide any required objects to instantiate a new object. This new object doesn’t need to construct or customize the objects themselves.
In Android, you might find you need to access the same complex objects from various points in your app, such as a network client, image loader, or SharedPreferences for local storage. You can inject these objects into your activities and fragments and access them right away.
Currently, there are three main libraries for dependency injection: Dagger ‘2’, Dagger Hilt, and Koin.
These design patterns are all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object patterns define ways to compose objects to obtain new functionality.
Facade
Adapter
Decorator
Composite
Protection Proxy
Facade:
The facade pattern is used to define a simplified interface to a more complex subsystem.
Adapter:
The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the “adaptee” with a class that supports the interface required by the client.
Decorator:
The decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behavior.
Composite:
The composite pattern is used to compose zero-or-more similar objects so that they can be manipulated as one object.
Protection Proxy:
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object. Protection proxy is restricting access.
Command
Observer
Strategy
State
Visitor
Mediator
Memento
Chain of Responsibility
Command:
The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.
Observer:
The observer pattern is used to allow an object to publish changes to its state. Other objects subscribe to be immediately notified of any changes.
Strategy:
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
State:
The state pattern is used to alter the behavior of an object as its internal state changes. The pattern allows the class for an object to apparently change at run-time.
Visitor:
The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.
Mediator:
The mediator design pattern is used to provide a centralized communication medium between different objects in a system. This pattern is very helpful in an enterprise application where multiple objects are interacting with each other.
Memento:
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
Chain of Responsibility:
The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Flexbox-Layout in Android
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
How to Get Current Location in Android?
Kotlin Setters and Getters