Android Design Pattern

Prachi Mishra
4 min readJul 23, 2022

--

Design Pattern will use to improve code reusability or clean the app architecture. Design patterns usually deal with objects.

The benefit of Design Pattern-

  • Design Patterns uses object-oriented concepts
  • Design Pattern will make our code cleaner and easy to understand by others.
  • Design Pattern can improve the reusability and extensibility of Android Apps. It means we don't need to write the same code again and again.
  • Design Pattern is stable and tested, so it's a good experience to use by developers.

Design pattern types:

  • Creational patterns: How you create objects.
  • Structural patterns: How you compose objects.
  • Behavioural patterns: How you coordinate object interactions.

1. Creational Pattern — This pattern is used to create an object without actually showing the logic or even the steps involved in the creation of this object.

Types-

1. Builder —
2. Dependency Injection
3. Singleton
4. Factory

Builder — As the name suggests, this pattern simplifies the creation of objects, which means we can create objects of the same class with different properties.

It handles the changes in the internal representation of objects. It can improve the flexibility and readability of source code.

In Android, take the example of AlertDialog-

AlertDialog.Builder(this)
.setTitle("")
.setMessage("Please use the spicy mustard.")

.setPositiveButton("OK") { dialogInterface, i ->
// "OK" action
}.show()

Dependency Injection —

DI to build upon the concept of Inversion of Control. That means a class should get its dependencies from outside.
This means, your class cannot instantiate another class using a new keyword inside it. Instead, you have to supply the object from outside.

Currently we have Dagger2, Hilt, Koin, ButterKnife etc as the main DI in Android.

Singleton

The Singleton Pattern guarantees a class has one instance only, and a global point of access to it is provided by that class. Whenever multiple classes or clients request that class, they get the same instance of the class.

The benefit of Singleton Pattern-

In Mobile Applications where we are using retrofit, shared preference, Gson or OkHttpClient if we will use more than one instantiation then it might be an issue for the App performance.

object SampleSinglton {
fun sampleMethod() {
// ...
}
}

When you need to access members of the singleton object, you make a call like this:

SampleSinglton.sampleMethod()

Factory —

The factory takes care of all the object creational logic. In this pattern, a factory controls which object to instantiate.

In the Factory pattern, we create an object without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

2. Structural Pattern — As the name suggests, in this pattern we will focus on the Structure of the code. This design pattern will help to understand the code just by looking into the structure of the code.

Types-

1. Adapter Pattern —
2. Decorator
3. Facade
4. Proxy

Adapter pattern — The adapter pattern is used to connect two or more incompatible interfaces. This pattern lets the classes work together.

Example — recycler View,PagerAdapter etc.

Facade — This pattern is used to simplify the interface to a complex subsystem. Example — MVP architecture (in model view presenter concept view or presenter is less bothering about the data )

Decorator — This Pattern is used to attach dynamically the responsibility of an object to extend the functionality at runtime. This provides a flexible alternative to using inheritance to modify behaviour.

3. Behaviour Pattern — In this design pattern, the interaction between objects should be easy to talk about and loosely coupled between classes.

This patterns can vary in scope , from the relationship between two objects to your app's entire architecture.

Types-

1. Chain of Responsibility Pattern

2. Command Pattern

3. Observer Pattern

4. Strategy Pattern

5. State Pattern

6. Template Pattern

7. Visitor Pattern

8. Null Object

Chain of Responsibility — this pattern is used to process requests, each of which may be dealt with by a different handler.

Command Pattern — We encapsulate the request and send to the receiver, but we don't know about the receiver, or we are not bothering about the task who is going to do, we just want the result. For example — Event Bus, Greenrobots etc.

Observer-This is the well known Pattern in Android . Here it defines a one to many dependencies between objects. When any changes in first object, it will notify the all other related object as well. For example — Live Data, Rx Java MVC, MVP architecture etc.

Strategy-when we have multiple objects of the same nature with different functionalities.

State Pattern — This pattern is used to alter the behaviour of an object as its internal state changes. This pattern allows the class for an object to change at run time.

App Architecture- App architecture can be based on S.O.L.I.D principle, Here we should take care of responsibility of each class, Folder organization and the structure of the code (network calls,responses ,errors.etc)

over the App Architecture, we have one more very famous technique that is Clean Architecture*

Clean Architecture- it defines the overall app architecture for more info refer my favourite link

Conclusion-

In this blog we tried to cover some important design pattern which we are using in Android, for example-Builder pattern, Singleton pattern, Dependency Injection pattern,Adapter pattern,Observer pattern, Command , MVP, MVVM, MVC pattern.

I took some examples from the resources which are available on Google.

Happy coding…

Follow me on LinkedIn :)

--

--

Prachi Mishra

I am working As an Android Dev. Currently, developing an App for my Dream car (BMW) .In my Dev path If I find something interesting I like to share it with you.