Design Patterns

What is a Design Pattern?

A Design Pattern is a general, reusable solution to a common problem within a given context in software design. It is not a finished design that can be directly transformed into code but rather a template for how to solve a problem in various situations. Design patterns capture best practices and provide a shared vocabulary for designers to communicate complex concepts effectively.

Inventor of Design Pattern

The Gang of Four (GoF) design patterns, introduced by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, are categorized into three groups and they are as follows:

  1. Creational Design Patterns – It is used for object creation mechanisms, trying to create objects in a manner suitable to the situation. It consists of 5 different types of design patterns.
  2. Structural Design Patterns – It is used for object composition, providing ways to compose objects to form larger structures. It consists of 7 different types of design patterns.
  3. Behavioral Design Patterns – It is used for object interaction and responsibilities, identifying common communication patterns between objects. It consists of 11 different types of design patterns.

So, in total, there are 23 design patterns available through which one can provide a templatized solution for the problems.

Advantages of Design Patterns

  1. Readability – They improve the readability of the code by providing standard solutions for common problems.
  2. Maintainability – Patterns make the system easier to understand and modify, leading to better maintainability.
  3. Flexibility – Patterns provide guidelines for structuring code in a way that is adaptable to changes.
  4. Reusability – Design patterns promote code reuse by providing tested, proven development paradigms.
  5. Communication – They provide a common language among developers, making it easier to discuss and refine system architecture.
  6. Efficiency – By using patterns, developers can avoid reinventing the wheel and focus on more critical project-specific problems.

Different Types of Design Patterns

  • Creational Design Pattern
    • Factory Method – Defines an interface for creating an object but lets subclasses alter the type of objects that will be created.
    • Abstract Factory – Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
    • Builder – Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
    • Prototype – Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.
    • Singleton – Ensures a class has only one instance and provides a global point of access to it.
  • Structural Design Pattern
    • Adapter – Converts the interface of a class into another interface clients expect. It allows classes to work together that couldn’t otherwise because of incompatible interfaces.
    • Bridge – Decouples an abstraction from its implementation so that the two can vary independently.
    • Composite – Composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.
    • Decorator – Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
    • Facade – Provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use.
    • Flyweight – Uses sharing to support large numbers of fine-grained objects efficiently.
    • Proxy – Provides a surrogate or placeholder for another object to control access to it.
  • Behavioral Design Pattern
    • Chain of Responsibility – Passes a request along a chain of handlers. Each handler either handles the request or passes it to the next handler in the chain.
    • Command – Encapsulates a request as an object, thereby allowing users to parameterize clients with different requests, queue or log requests, and support undoable operations.
    • Interpreter – Defines a grammatical representation of a language and provides an interpreter to deal with this grammar.
    • Iterator – Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
    • Mediator – Defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly.
    • Memento – Captures and externalizes an object’s internal state so that the object can be restored to this state later, without violating encapsulation.
    • Observer – Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
    • State – Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
    • Strategy – Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.
    • Template Method – Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing its structure.
    • Visitor – Represents an operation to be performed on the elements of an object structure. It lets you define a new operation without changing the classes of the elements on which it operates.

Most commonly used Design Patterns in day-to-day life

  1. Singleton Pattern – Ensures that a class has only one instance and provides a global point of access to it.
  2. Factory Method Pattern – Defines an interface for creating an object but lets subclasses decide which class to instantiate.
  3. Observer Pattern – Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
  4. Decorator Pattern – Adds behavior to an individual object, dynamically and without affecting the behavior of other objects from the same class.
  5. Strategy Pattern – Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
  6. Command Pattern – Encapsulates a request as an object, thereby allowing parameterization of clients with different requests, queuing of requests, and logging of requests.
  7. Adapter Pattern – Converts the interface of a class into another interface that clients expect.
  8. Proxy Pattern – Provides a surrogate or placeholder for another object to control access to it.
  9. Template Method Pattern – Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
  10. Builder Pattern – Separates the construction of a complex object from its representation so that the same construction process can create different representations.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *