Design Patterns

Published On: 3 July 2022.By .
  • General

In this article, we are going to take a little introduction to Design Patterns. As we know, for building a Strong home, a good base is needed, Similarly for developing better software, to make your software architecture flexible,  we need to follow some principles or patterns called Design patterns.

What are Design Patterns?
Design patterns are typical solutions to common and complex problems in software design. Each pattern is like a blueprint that you can customize to solve a problem in your development process.

Need of Design Patterns?

  • It teaches you how to solve all sorts of problems using the principle of object-oriented programming. 
  • It is easy to understand the pattern of code, and it makes communication more efficient between your teammates, as when anyone suggests using Singleton, then we will easily know about Singleton.

 

Criticism of Design Patterns
In Software Engineering, Everything has some negative points, so in the case of design patterns. It is not guaranteed that simply knowing and implementing design patterns will create a well-structured piece of software.

  • If you overuse design patterns, your project can become more complex.
  • Many Desing Patterns are redundant by Many Programming Languages.

 

Classification of Design Patterns

  • Creational Design Pattern: It describes how to create or instantiate objects. Examples of creational Design patterns are Singleton, Builder, and Prototype.
  • Structural Design Pattern: It describes how objects are composed and combined to form larger structures. An example includes Model-View-Controller (MVC) etc.
  • Behavioral Design Pattern: It describes how objects communicate with each other. Examples include Delegation, Strategy, and Observer.

 

In this article, we will describe one design pattern from each classification. factory method from creational, adapter method from the structural pattern, and command method from Behavioral pattern.

So, let’s start with the factory method first.

Factory Design Pattern

This pattern allows the subclass to make a decision for which class to instantiate as the creation of an object is done by an abstract class or an interface.
This patterning method is also known as Virtual Constructor.

Advantages of Factory Desing Pattern:

  • It allows the subclass to choose the type of object to create at run-time.
  • It promotes loose coupling.
  • It separates the product construction code from the code that actually uses that code.
  • This method uses an interface or abstract class, so in next time, you just need to create a subclass and extend the interface into a subclass.

 

Disadvantages of Factory Desing Pattern:
The code becomes more complex as we need to introduce a lot of new classes to implement the pattern.

We are going to create an interface OS. Now, we have various classifications of OS like Android, IOS, Windows, etc.
On the upper level, an OS has various methods like specification, version, and other things.
Firstly, we create a basic interface called OS whose implementation is as follows:

We also create another class OSfactory class, which code is as follows:

Similarly create three classes for Android, IOS, and windows.
So, below is the implementation of the Main class FactoryDesingPattern.java.

 

Adapter Design Pattern

An Adapter pattern says that “Just convert the interface of a class into another interface that a client wants”.
An adaptor pattern allows objects with incompatible interfaces to collaborate.
The Adapter pattern works as a middle layer class that serves as a translator between your code, and legacy class.
For Example, Suppose you are creating a stock market application in which you want to show some 3d analytics charts by taking data from the stock providers. Now that data is in the format of XML.

                                                                 3rd party library
                                                                 Accepts only JSON
Stock Data ——–> XML format Data ———————————> X (process can’t be completed)


Now, the adapter layer comes, a class that works to convey XML data to JSON data.


Stock Data —-> XML format Data—–                                         |–> 3rd party lib.
                                                                |                                         |
                                                                |———–>XML to JSON—|
                                                                                Converter

Advantages of Factory Desing Pattern:

  • Single Responsibility code. You can separate the code from your business logic.
  • Open/Closed Principle. You can introduce the new types of adapters into the code without breaking the existing client code. 


Disadvantages of Factory Desing Pattern:
The overall Complexity of code increases as you need to introduce some new classes and interfaces.

 

    Command Design Pattern

    The command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as to method arguments, delay or queue a request’s execution, and support undoable operations.

    Let’s understand it by an example, Suppose you get to a nice restaurant and sit at the table by the window. A friendly waiter approaches you and quickly takes your order, writing it down on a piece of paper. The waiter goes to the kitchen and sticks the order on the wall. After a while, the order gets to the chef, who reads it and cooks the meal accordingly. The cook places the meal on a tray along with the order. The waiter discovers the tray, checks the order to make sure everything is as you wanted it, and brings everything to your table.

    The paper order serves as a command. It remains in a queue until the chef is ready to serve it. The order contains all the relevant information required to cook the meal. It allows the chef to start cooking right away instead of running around clarifying the order details from you directly.

    Advantages of Factory Desing Pattern:

    • Single Responsibility Principle: You can decouple classes that invoke operations from classes that perform these operations.
    • Open/Closed Principle. You can introduce the new commands into the app without breaking the existing client code. 
    • You can implement UNDO/REDO.
    • You can implement deferred execution of operations.


      Disadvantages of Factory Desing Pattern:
      The code can become more complicated since you are introducing some new layers between senders and receivers.

      So, In this article, we learned about three important Design patterns which include Command, Factory, and Adapter Design patterns.

      Here are some references.

        Thanks.

        Related content

        That’s all for this blog