Design Patterns in Android with Kotlin
Last Updated :
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.
Now we are going to discuss the most important design patterns that you should know.
1. Creational Patterns
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 Borgsjö 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.
2. Structural Patterns
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.
3. Behavioral Patterns
- 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.
Similar Reads
Retrofit with Kotlin Coroutine in Android Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is popular among the developers to use the API key. The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can e
3 min read
SearchView in Android with Kotlin SearchView is a widget in android which provides a search interface with the help of which users can be able to make searches within the given list of data. In this search view, the user has to specify the search query. According to the search, query results will be populated within the listview. In
4 min read
Calendar View App in Android with Kotlin Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used. In this article, we will take a look at How to implement Calendar View within our Android application using Kotlin. A sample video is g
3 min read
Android SQLite Database in Kotlin Android comes with an inbuilt implementation of a database package, which is SQLite, an open-source SQL database that stores data in form of text in devices. In this article, we will look at the implementation of Android SQLite in Kotlin. SQLite is a self-contained, high-reliability, embedded, full-
5 min read
Android Mastery with Kotlin: Beginner to Advanced Are you looking to build your first Android app, or perhaps youâre a seasoned developer seeking to enhance your skills with a modern language? Have you heard about Kotlin but arenât sure why itâs become the preferred choice for Android development? With its clear syntax, powerful features, and seaml
5 min read