5 Design Patterns Every Android Developer Must Know!

5 Design Patterns Every Android Developer Must Know!

Design Patterns for Android Developer.

Maheshwar Ligade's photo
Maheshwar Ligade
·May 17, 2022·

6 min read

Table of contents

In this article, I explain the whats design pattern, and as an android developer whether you are fresher, experienced, or master, you should know these below design patterns.

original.jpeg

What's the design Pattern:

A design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.

A design pattern is a solution to commonly recurring problems.

Design patterns are not finished products; rather, they are templates that can be applied to multiple situations based on context and can be improved over time, making a very robust software engineering tool. Design pattern helps developers to implement proven and widely adopted solution. It helps to improve development speed, and prototyping solutions faster developers using design pattern templates can improve coding efficiency, maintainability, and code readability.

Types of Desing Pattern:

Below are the different types of design patterns:

  1. Creational Desing pattern
  2. Structural Desing pattern
  3. Behavioral Desing pattern.

1. Creational:

These patterns are designed for ways to create objects or classes.

2. Structural:

These patterns are designed with regard to a class's structure and composition.

3. Behavioral:

These patterns are designed depending on how one class communicates with others.

Desing Pattern for Android Dev:

Android is a mobile developer platform, which is also providing APIs to develop apps for watches, and embedded systems. As if you are an android developer you should know common simple design patterns.

Pattern #1 Singleton:

This creational design pattern helps to instantiate objects once and only one time in the application context. A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. This is useful when you want to create a database connection object to create one and only single session from the application.

Remember the below Points:

  1. A private constructor
  2. A static reference of its class
  3. One static method
  4. Globally accessible object reference
  5. Consistency across multiple threads

Example:

public class SingletonInstance {

//A static reference of its class
    private static SingletonInstance instance = null;

//private constructor
    private SingletonInstance() {

    }
//One static method
//Globally accessible object reference

    public static SingletonInstance getInstance() {
        if (instance == null) {

//Consistency across multiple threads

            synchronized (SingletonInstance.class) {
                if (instance == null) {
                    instance = new SingletonInstance();
                }
            }
        }
        return instance;
    }
}

Pattern #2 Factory:

This is also a creational design pattern. This makes sure that the subclasses should decide how to instantiate the object. We create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object. You can have any number of classes but those hides and objects are created with the use of interfaces or abstract classes.

Remember the below point:

  1. Implementation.
  2. Create an interface.
  3. Create concrete classes implementing the same interface.
  4. Create a Factory to generate an object of concrete class based on input.
  5. Use the Factory to get objects of the concrete class.

Example:


//CarType.java
public enum CarType {
    SMALL, SEDAN, LUXURY
}

//Car.java
public abstract class Car {

  public Car(CarType model) {
    this.model = model;
    arrangeParts();
  }

  private void arrangeParts() {
    // Do one time processing here
  }


  protected abstract void construct();

  private CarType model = null;

  public CarType getModel() {
    return model;
  }

  public void setModel(CarType model) {
    this.model = model;
  }
}

//LuxuryCar.java


public class LuxuryCar extends Car {

  LuxuryCar() {
    super(CarType.LUXURY);
    construct();
  }

  @Override
  protected void construct() {
    System.out.println("Building luxury car");

  }
}

//SmallCar.java
public class SmallCar extends Car {

  SmallCar() {
    super(CarType.SMALL);
    construct();
  }

  @Override
  protected void construct() {
    System.out.println("Building small car");

  }
}

Pattern #3 Builder:

Builder pattern aims to Separate the construction of a complex object from its representation so that the same construction process can create different representations. It is used to construct a complex object step by step and the final step will return the object. The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems.

Remember the below point:

  1. A private constructor
  2. An inner class usually called Builder
  3. function for each field to set the field value return
  4. function build return instance of the Main class

Example Kotlin:

class Hamburger private constructor(
    val cheese: Boolean,
    val beef: Boolean,
    val onions: Boolean
) {
    class Builder {
        private var cheese: Boolean = true
        private var beef: Boolean = true
        private var onions: Boolean = true

        fun cheese(value: Boolean) = apply { cheese = value }
        fun beef(value: Boolean) = apply { beef = value }
        fun onions(value: Boolean) = apply { onions = value }

        fun build() = Hamburger(cheese, beef, onions)
    }
}

Pattern #4 Adapter:

As an android developer, you may have used this design pattern. This is like the daily bread of android developers. The adapter pattern works as a bridge between two incompatible interfaces. This pattern involves a single class that is responsible to join functionalities of independent or incompatible interfaces. With a real-life example, you can map this pattern with your cell phone charger adapter. This is the type of Structural design pattern.


interface Bird
{
    public void fly();
    public void makeSound();
}

class Sparrow implements Bird
{
    public void fly()
    {
        System.out.println("Flying");
    }
    public void makeSound()
    {
        System.out.println("Chirp Chirp");
    }
}

interface ToyDuck
{
    // target interface
    // toyducks dont fly they just make
    // squeaking sound
    public void squeak();
}

class PlasticToyDuck implements ToyDuck
{
    public void squeak()
    {
        System.out.println("Squeak");
    }
}

class BirdAdapter implements ToyDuck
{
    // You need to implement the interface your
    // client expects to use.

    Bird bird;
    public BirdAdapter(Bird bird)
    {
        this.bird = bird;
    }

    public void squeak()
    {
        bird.makeSound();
    }
}

Pattern #5 Facade:

A facade design pattern is a very important design pattern in enterprise development. The facade pattern provides a higher-level interface that makes a set of other interfaces easier to use.

Facade defines a new interface, whereas Adapter uses an old interface. Facade deals with interfaces, not implementation.

Example:

public interface LaptopShop {  
    public void modelNo();  
    public void price();  
}  

public class DellLaptop implements LaptopShop {  
    @Override  
    public void modelNo() {  
        System.out.println(" Dell 6 ");  
    }  
    @Override  
    public void price() {  
    System.out.println(" Rs 65000.00 ");  
    }  
}  

public class Lenovo implements LaptopShop {  
    @Override  
    public void modelNo() {  
    System.out.println(" Lenovo 3 ");  
    }  
    @Override  
    public void price() {  
        System.out.println(" Rs 45000.00 ");  
    }  
}  


public class ShopKeeper {  
    private LaptopShop dell;  
    private LaptopShop lenovo;  

    public ShopKeeper(){  
        dell= new DellLaptop();  
        lenovo=new Lenovo();  

    }  
    public void dellSale(){  
        dell.modelNo();  
        dell.price();  
    }  
        public void lenovoSale(){  
        lenovo.modelNo();  
        lenovo.price();  
    }  
}

Conclusion:

In this article, I try to explain a very basic list which you need to know. This article doesn't claim only these design patterns are important but these are the basics you should familiar with.

This is just a basic list there are other designs and architectural patterns such as Dependency injection, MVVM, MVP, and MVC at least you need to know.

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

==========================**=========================

If this article adds any value for you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this