A design pattern provides a general solution to problems that we face in creating software design. It provides a template for solving a problem that can be re-used in multiple situations as well.

Let us check some most commonly used design patterns:

  1. Factory Pattern
    Factory design pattern is used to create objects without exposing the logic of how the object is created. This is mostly used in situations when we want to create an object that represents application configuration. The configuration is defined in some file (xml, json) in the background. For example:

    Document doc = DocumentFactory.getEmail()

    Here is an example image:
    factory-design-pattern

  2. Proxy Pattern
    In this design pattern, an object represents another object.
    Let us say, we have a class A which is heavy and requires lot of resource to instantiate (for example reads lot of configuration), and we don’t want users to instantiate it again and again.
    For this, we will introduce another class B (proxy to A) that has methods to instantiate A. Now, B will make sure that A will be instantiated only once and for further calls, the existing A object will be returned.
    proxy-design-pattern
  3. Repository Pattern
    The repository design pattern comes into play when we have multiple data sources. It would be illogical for an application to maintain connection to multiple data sources. Also, if we need to update the data source, the application code has to be updated as well.
    The repository pattern exposes an API through which other applications will connect. In this way, applications don’t have to worry about type and location of multiple data sources.
    repository-design-pattern
  4. Command Pattern
    In command design pattern, we setup a middle ware that receives an object with data. The purpose of this middle ware is to execute the methods of the received object ( execute commands ) without knowing anything about the object.
    The command objects represents classes that implement some interface. The middle ware specifies the interface as parameter to receive the command object. It then executes the methods with the help of this interface reference.
    command-design-pattern
  5. Façade Pattern
    Façade means front or face of something.
    This command pattern is used to provide an interface to a system in the background. This helps in hiding the complexity of the system from the client.
    It sounds similar to factory design pattern but both have different purpose.
    Client => Factory Pattern => Creates objects
    Client => Facade Pattern => Access a system
    facade-design-pattern
  6. Observer Pattern
    Observer pattern is used to notify software components when there is a state change of an existing component. An object called Subject maintains a list of all the observers who needs to be notified by calling their method(s) when there is state change (value of member variable changed) of an existing object.
    observer-design-pattern
  7. Singleton Pattern
    Singleton design pattern forces only one instance per class. The instance of the class is created inside the class only as static and can be accessed outside with the help of some method.
    singleton-design-pattern
  8. Adapter Pattern
    In this design pattern, a new class is created that acts as an interface between existing classes/interfaces and the client code. This is helpful in situations when our code is dependent on API that changes frequently.
    adapter-design-pattern
  9. Bridge Pattern
    Bridget pattern is used when we want to connect two software components that are totally independent of each other. Java Database Connectivity is one of the examples that implements bridge design pattern.
    bridge-design-pattern
  10. Decorator Pattern
    This design pattern allows users to add new functionality as well as update existing functionality of an object (decorate) without changing the structure of existing class.
    In this pattern, we create a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
    decorator-design-pattern
  11. Null Design Pattern
    This design pattern is used to return a default object instead of null when data we are looking is not found.
    Here, we create an interface that is implemented by the actual class as well as by another class created for representing null objects.
    A customer factory is placed that creates objects depending on data found.
    null-design-pattern