Design Patterns

A design patterns are Solution to general problem occurred in software design and development. These solutions were obtained by trial and error by software developers over quite a substantial period of time. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.


Uses of Design Patterns.

Following are some of the common usage of the design pattern in software development:

  1. Design patterns can speed up the development process by providing tested, proven development paradigms.

  2. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.

  3. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.

  4. Patterns allow developers to communicate using well-known, well understood names for software interactions.

  5. Common design patterns can be improved over time, making them more robust than ad-hoc designs.


Types of Design Patterns


There are mainly three types of design patterns:


1. Creational

These design patterns are all about class instantiation or object creation. These patterns can be further categorized into Class-creational patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done. Creational design patterns are the Factory Method, Abstract Factory, Builder, Singleton, Object Pool, and Prototype.


Further, the Creational Design Pattern have the following patterns:

  1. Factory Pattern

  2. Abstract Factory Pattern

  3. Singleton Pattern

  4. Prototype Pattern

  5. Builder Pattern.

Use case of creational design pattern-

1) Suppose a developer wants to create a simple DBConnection class to connect to a database and wants to access the database at multiple locations from code, generally what developer will do is create an instance of DBConnection class and use it for doing database operations wherever required. Which results in creating multiple connections from the database as each instance of DBConnection class will have a separate connection to the database. In order to deal with it, we create DBConnection class as a singleton class, so that only one instance of DBConnection is created and a single connection is established. Because we can manage DB Connection via one instance so we can control load balance, unnecessary connections, etc. 2) Suppose you want to create multiple instances of similar kind and want to achieve loose coupling then you can go for Factory pattern. A class implementing factory design pattern works as a bridge between multiple classes. Consider an example of using multiple database servers like SQL Server and Oracle. If you are developing an application using SQL Server database as back end, but in future need to change database to oracle, you will need to modify all your code, so as factory design patterns maintain loose coupling and easy implementation we should go for factory for achieving loose coupling and creation of similar kind of object.


2. Structural

These design patterns are about organizing different classes and objects to form larger structures and provide new functionality. Structural design patterns are Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Private Class Data, and Proxy.


The Structural Design Pattern in Classified in the following design patterns:

  1. Adapter Pattern

  2. Bridge Pattern

  3. Composite Pattern

  4. Decorator Pattern

  5. Facade Pattern

  6. Flyweight Pattern

  7. Proxy Pattern

Use Case Of Structural Design Pattern- 1) When 2 interfaces are not compatible with each other and want to make establish a relationship between them through an adapter its called adapter design pattern. Adapter pattern converts the interface of a class into another interface or classes the client expects that is adapter lets classes works together that could not otherwise because of incompatibility. so in these type of incompatible scenarios, we can go for the adapter pattern.


3. Behavioral

Behavioral patterns are about identifying common communication patterns between objects and realize these patterns. Behavioral patterns are Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template method, Visitor


The Behavioral design pattern is classified in the following patterns:

  1. Chain Of Responsibility Pattern

  2. Command Pattern

  3. Interpreter Pattern

  4. Iterator Pattern

  5. Mediator Pattern

  6. Memento Pattern

  7. Observer Pattern

  8. State Pattern

  9. Strategy Pattern

  10. Template Pattern

  11. Visitor Pattern

Use Case of Behavioral Design Pattern- 1) Template pattern defines the skeleton of an algorithm in an operation deferring some steps to sub-classes, Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm structure. say for an example in your project you want the behavior of the module can be extended, such that we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications. However, No one is allowed to make source code changes to it. it means you can add but can’t modify the structure in those scenarios a developer can approach template design pattern.


All the sub- categories of the Design Patterns will explained in the next article.


Sofia Sondh


The Tech platform

www.thetechplatform.com


Recent Posts

See All

Singleton Design Pattern with Java

Singleton Design Pattern As the name suggests “single” i.e has only one instance and provides a global instance. Singleton design pattern restricts the instantiation of a class and ensures that one ob