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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read