Module 3: Design Patterns (C++)
Reusable Solutions for Common Software
Design Problems
Introduction to Design Patterns
• Design Patterns capture proven software
design solutions
• Encourage reusable and maintainable
architecture
• Origin: 'Gang of Four' (GoF)
Classification of Patterns
• 1. Creational – Object creation mechanisms
• 2. Structural – Object composition
• 3. Behavioral – Object communication
• Example Patterns:
• Creational: Factory, Singleton
• Structural: Bridge, Flyweight
• Behavioral: Iterator, Observer, MVC
Creational Patterns Overview
• Focus on object creation and abstraction.
• Common patterns:
• Abstract Factory
• Factory Method
• Singleton
Abstract Factory Pattern
• Intent: Provide interface for creating families
of related objects without specifying classes.
• Example (C++):
• class Button { public: virtual void paint()=0; };
• class WinButton: public Button { void
paint(){cout<<"Windows Button";}};
Factory Method Pattern
• Intent: Define an interface for creating objects
but let subclasses decide which class to
instantiate.
• Example:
• class Document { virtual void createPage()=0;
};
• class Report: public Document { void
createPage(){cout<<"Report Page";}};
Singleton Pattern
• Intent: Ensure a class has only one instance.
• Example:
• class Singleton {
• static Singleton* instance;
• Singleton(){};
• public:
• static Singleton* getInstance(){ if(!instance)
instance=new Singleton(); return instance; }
• };
Structural Patterns Overview
• Focus on class and object composition.
• Examples:
• • Bridge Pattern
• • Flyweight Pattern
Bridge Pattern
• Intent: Decouple abstraction from
implementation.
• Example:
• class Device { virtual void turnOn()=0; };
• class TV: public Device { void
turnOn(){cout<<"TV On";}};
• class Remote { Device* device; public:
Remote(Device*d):device(d){} void
pressButton(){device->turnOn();}};
Flyweight Pattern
• Intent: Minimize memory use by sharing data
among objects.
• Example:
• class Character{char symbol;public:
Character(char c):symbol(c){} void
display(){cout<<symbol;}};
Behavioral Patterns Overview
• Focus on object interaction.
• Examples:
– Iterator
– Observer
– Model-View-Controller (MVC)
Iterator Pattern
• Intent: Access elements sequentially without
exposing representation.
• Example:
• vector<int> nums={1,2,3}; for(auto
it=[Link]();it!=[Link]();++it)
cout<<*it;
Observer Pattern
• Intent: Define dependency so when one
object changes, others are notified.
• class Observer{virtual void update()=0;};
• class Subject{vector<Observer*>obs; void
notify(){for(auto o:obs)o->update();}};
Model–View–Controller (MVC)
Pattern
• Separates concerns:
– Model – data
– View – UI
– Controller – handles input
• class Model{int data;}; class View{void
show(int d){cout<<d;}}; class
Controller{Model*m;View*v;void
updateView(){v->show(m->data);}};
Summary
• Creational patterns manage object creation.
• Structural patterns organize object
relationships.
• Behavioral patterns coordinate
communication.
• Patterns improve reusability, flexibility, and
scalability.