C++ Design Patterns - Interview Cheat Sheet
1. Singleton Pattern (Creational)
Real-World Analogy:
Only one Prime Minister in a country.
Problem:
Ensure only one instance of a class is created.
Code:
class Singleton {
private:
static Singleton* instance;
Singleton() {} // private constructor
public:
static Singleton* getInstance() {
if (!instance)
instance = new Singleton();
return instance;
}
};
Singleton* Singleton::instance = nullptr;
2. Factory Method Pattern (Creational)
Real-World Analogy:
Ordering food: You ask for a pizza, and factory gives correct type.
Problem:
Create objects without specifying the exact class.
Code:
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() { /* draw circle */ }
};
class Square : public Shape {
public:
void draw() { /* draw square */ }
};
class ShapeFactory {
public:
Shape* createShape(const std::string& type) {
if (type == "circle") return new Circle();
if (type == "square") return new Square();
return nullptr;
}
C++ Design Patterns - Interview Cheat Sheet
};
3. Observer Pattern (Behavioral)
Real-World Analogy:
YouTube: Subscribers get notified when channel updates.
Problem:
One-to-many dependency. Notify observers when state changes.
Code:
class Observer {
public:
virtual void update(int value) = 0;
};
class Subject {
std::vector<Observer*> observers;
int state;
public:
void attach(Observer* obs) { observers.push_back(obs); }
void setState(int val) {
state = val;
for (auto* obs : observers) obs->update(state);
}
};
4. Strategy Pattern (Behavioral)
Real-World Analogy:
Payment method: Card, UPI, or Cash.
Problem:
Change algorithm behavior at runtime.
Code:
class PaymentStrategy {
public:
virtual void pay() = 0;
};
class PayByCash : public PaymentStrategy {
public:
void pay() { /* Cash payment */ }
};
class PayByUPI : public PaymentStrategy {
public:
void pay() { /* UPI payment */ }
};
class PaymentContext {
PaymentStrategy* strategy;
public:
void setStrategy(PaymentStrategy* s) { strategy = s; }
C++ Design Patterns - Interview Cheat Sheet
void execute() { strategy->pay(); }
};
5. Adapter Pattern (Structural)
Real-World Analogy:
Mobile charger adapter converts plug type.
Problem:
Bridge mismatch between incompatible interfaces.
Code:
class OldPrinter {
public:
void oldPrint() { /* prints in old way */ }
};
class PrinterInterface {
public:
virtual void print() = 0;
};
class Adapter : public PrinterInterface {
OldPrinter* old;
public:
Adapter(OldPrinter* o) : old(o) {}
void print() { old->oldPrint(); }
};
6. Decorator Pattern (Structural)
Real-World Analogy:
Wrapping a gift box again and again.
Problem:
Add responsibilities to objects at runtime.
Code:
class Coffee {
public:
virtual std::string cost() = 0;
};
class SimpleCoffee : public Coffee {
public:
std::string cost() { return "5$"; }
};
class MilkDecorator : public Coffee {
Coffee* base;
public:
MilkDecorator(Coffee* b) : base(b) {}
std::string cost() { return base->cost() + " + Milk"; }
};
C++ Design Patterns - Interview Cheat Sheet
7. Command Pattern (Behavioral)
Real-World Analogy:
TV remote: Button encapsulates a command.
Problem:
Encapsulate a request as an object.
Code:
class Command {
public:
virtual void execute() = 0;
};
class Light {
public:
void on() { /* turn on */ }
};
class LightOnCommand : public Command {
Light* light;
public:
LightOnCommand(Light* l) : light(l) {}
void execute() { light->on(); }
};
8. Builder Pattern (Creational)
Real-World Analogy:
Pizza builder step-by-step.
Problem:
Construct complex object step-by-step.
Code:
class Pizza {
public:
void addTopping(std::string t) { /* add topping */ }
};
class PizzaBuilder {
Pizza* pizza;
public:
PizzaBuilder() { pizza = new Pizza(); }
void addCheese() { pizza->addTopping("cheese"); }
void addVeggies() { pizza->addTopping("veggies"); }
Pizza* getPizza() { return pizza; }
};
9. Prototype Pattern (Creational)
Real-World Analogy:
Photocopy: Cloning an object.
C++ Design Patterns - Interview Cheat Sheet
Problem:
Copy object without knowing exact type.
Code:
class Prototype {
public:
virtual Prototype* clone() = 0;
};
class ConcretePrototype : public Prototype {
public:
Prototype* clone() {
return new ConcretePrototype(*this);
}
};
10. State Pattern (Behavioral)
Real-World Analogy:
Fan speed: Low, Medium, High states.
Problem:
Change behavior based on internal state.
Code:
class State {
public:
virtual void handle() = 0;
};
class Fan {
State* state;
public:
void setState(State* s) { state = s; }
void pressButton() { state->handle(); }
};