Design patterns

Introduction To Design Patterns


The common problems that occur in software design,a design pattern provides a reusable solution for that. The pattern shows relationships & interactions between classes or objects. The idea is to speed up the development process by providing well-tested, demonstrated development/design paradigms. By using design patterns, you can make your code more pliable, recyclable, and sustainable. Design patterns are programming language independent strategies for solving a common problem. That means a design pattern appear for an idea, not a particular execution.

It’s not essential to always implement design patterns in your project. Design patterns are meant for common problem-solving where design patterns are not meant for project development. Whenever there is a need, you have to implement a acceptable pattern to avoid such problems in the future. You just have to try to understand the design patterns and their purposes, to find out which pattern to use where. Only by doing that, you will be able to pick the accurate one.



Goal:


To understand the purpose and usage of each design pattern in order to pick and implement the correct pattern as required.



Example:


In many real-world affairs, we want to create only one instance of a class. For example, there can be only one active president of a country at any given time. This pattern is called a Singleton pattern. Other software examples could be a single DB connection shared by multiple objects as creating a separate DB connection for every object is costly. Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers.


The Catalog of Design Patterns


Types of Design Patterns

There are chiefly three types of design patterns:


1. Creational

Creational design patterns are all about class instantiation or object creation. These

patterns can be further classified 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.

Use case of creational design pattern-


1) Suppose a inventor 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 the developer will do is create an instance of DBConnection class and use it for doing database operations wherever required.

This 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, we can control load balance, nonessential connections, etc.

2) Suppose you want to create multiple instances of a 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 Oracle & SQL Server. If you are developing an application using SQL Server database as back end, but in the future need to change the database to the oracle, you will need to modify all your code, so as factory design patterns maintain loose coupling and easy execution, we should go for the factory design pattern in order to achieve loose coupling and the creation of a similar kind of object.




Creational Design Patterns


Factory Method



It provides an interface for creating objects in a superclass, but authorizes subclasses to alter the type of objects that will be created.

It lets you make families of related objects without specifying their concrete classes.


refactoring.guru

Builder


The pattern allows you to produce different types and representations of an object using the same building code. It lets you build complex objects step by step.


refactoring.guru

Prototype


It lets you copy existing objects without making your code dependent on their classes.


refactoring.guru

Singleton


It lets you ensure that a class has only one instance, while providing a global access point to this instance.


refactoring.guru

2. Structural


The main task of design patterns is about put in order different classes and objects to form larger structures and provide new functionality.

Structural design patterns are Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Private Class Data, and Proxy.



Use Case Of Structural Design Pattern-

1) When 2 interfaces are not compatible with each other and want to set up a relationship between them through an adapter it’s called an adapter design pattern. The adapter pattern converts the interface of a class into another interface or class that the client expects, i.e adapter lets classes work together that could not otherwise because of incompatibility , so in these types of irreconcilable scheme, we can go for the adapter pattern.


Structural Design Patterns


Structural patterns describes how to build objects and classes into magnificient structures while keeping these structures flexible and efficient.


Adapter


It allows objects with irreconcilable interfaces to collaborate.

www.geeksforgeeks.org

Bridge

It lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.


www.geeksforgeeks.org

Composite


It lets you compose objects into tree structures and then work with these structures as if they were individual objects.


www.geeksforgeeks.org

Decorator


It lets you fix new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.


www.geeksforgeeks.org

Façade


It provides a simplified interface to a library, a framework, or any other complex set of classes.


www.geeksforgeeks.org

Flyweight

It lets you fit more objects into the available amount of RAM by dividing common parts of state between multiple objects instead of keeping all of the data in each object.


www.geeksforgeeks.org

Proxy

It lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.


www.geeksforgeeks.org

3. Behavioral


Behavioral patterns are all about identifying common communication patterns between objects and realizing these patterns.

Behavioral patterns are Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template method, Visitor



Use Case of Behavioral Design Pattern-

1) The template method lets subclasses redefine certain steps of an algorithm without changing the algorithm structure. The template pattern defines the skeleton of an algorithm in an operation deferring some steps to sub-classes. For example, in your project, you want the behavior of the module to be able to expand, such that we can make the module behave in new and different ways as to meet the needs of new applications. However, no one is allowed to make source code changes to it, i.e you can add but can’t modify the structure in those scenarios a developer can approach template design pattern.


Behavioral Design Patterns



Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.



Chain of Responsibility

It lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.


refactoring.guru

Command

It turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request's execution, and support undoable operations.


refactoring.guru

Iterator

It lets you traverse elements of a collection without exposing its fundamental representation (list, stack, tree, etc.).


refactoring.guru

Mediator

It lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.


refactoring.guru

Memento


It lets you save and restore the previous state of an object without revealing the details of its execution.


refactoring.guru

Observer

It lets you define a subscription mechanism to inform multiple objects about any events that happen to the object they're observing.


refactoring.guru

State

It lets an object alter its way of acting when its internal state changes. It appears as if the object changed its class.


refactoring.guru

Strategy


It lets you define a family of algorithms, put each of them into a separate class, and make their objects replaceable.


refactoring.guru

Template Method


It defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its composition.


refactoring.guru

Visitor

It lets you separate algorithms from the objects on which they work.


refactoring.guru