Design Patterns are proven, reusable solutions to common problems in software design. These are best practices developed by experienced object-oriented designers to facilitate scalability, modularity, and maintainability in software architecture.
Design patterns are templates for solving commonly recurring problems in software design. They are not code, but rather conceptual solutions that can be adapted to specific needs.
They were popularized by the "Gang of Four" (GoF) in their book "Design Patterns: Elements of Reusable Object-Oriented Software".
GoF patterns are classified into three main categories:
Concerned with the way objects are created.
Pattern | Purpose |
---|---|
Singleton | Ensures a class has only one instance and provides a global access point. |
Factory Method | Delegates instantiation to subclasses. |
Abstract Factory | Produces families of related objects without specifying their concrete classes. |
Builder | Constructs complex objects step by step. |
Prototype | Clones existing objects instead of creating new ones from scratch. |
Deal with the composition of classes and objects.
Pattern | Purpose |
---|---|
Adapter | Converts one interface into another expected by the client. |
Bridge | Decouples abstraction from implementation. |
Composite | Treats individual objects and compositions uniformly. |
Decorator | Adds responsibilities to objects dynamically. |
Facade | Provides a simplified interface to a complex subsystem. |
Flyweight | Shares common state between many objects to save memory. |
Proxy | Controls access to another object. |
Focus on communication between objects.
Pattern | Purpose |
---|---|
Observer | Notifies multiple objects when one changes state. |
Strategy | Allows selecting an algorithm at runtime. |
Command | Encapsulates a request as an object. |
Chain of Responsibility | Passes request along a chain of handlers. |
Mediator | Centralizes complex communications between objects. |
State | Changes behavior based on internal state. |
Template Method | Defines the skeleton of an algorithm in a method. |
Iterator | Provides a way to access elements sequentially. |
Visitor | Adds new operations to object structure without modifying it. |
Ensures only one instance of a class is created.
class Singleton {
private:
static Singleton* instance;
Singleton() {}
public:
static Singleton* getInstance() {
if (!instance)
instance = new Singleton();
return instance;
}
};
Use when: You need a single shared resource like a config manager, logger, or connection pool.
Delegates the instantiation process to subclasses.
class Product {
public:
virtual void use() = 0;
};
class ConcreteProduct : public Product {
public:
void use() override {
cout << "Using product" << endl;
}
};
class Creator {
public:
virtual Product* createProduct() = 0;
};
class ConcreteCreator : public Creator {
public:
Product* createProduct() override {
return new ConcreteProduct();
}
};
Use when: You want to decouple object creation from implementation.
Notifies dependent objects about state changes.
class Observer {
public:
virtual void update() = 0;
};
class Subject {
vector<Observer*> observers;
public:
void attach(Observer* obs) {
observers.push_back(obs);
}
void notify() {
for (auto& obs : observers)
obs->update();
}
};
Use when: An object should automatically notify others when its state changes (e.g., UI listeners, event systems).
Encapsulates algorithms and makes them interchangeable.
class Strategy {
public:
virtual void execute() = 0;
};
class ConcreteStrategyA : public Strategy {
public:
void execute() override {
cout << "Algorithm A" << endl;
}
};
class Context {
Strategy* strategy;
public:
Context(Strategy* s) : strategy(s) {}
void run() {
strategy->execute();
}
};
Use when: You want to change behavior at runtime without modifying code.
Design Patterns are an indispensable part of software engineering. By mastering them, developers can write more robust, extensible, and maintainable code. They offer shared vocabulary and design elegance, helping teams solve problems consistently.
π‘ βDesign patterns are not about reusing code but about reusing experience.β β Erich Gamma (GoF)
its working.