0% found this document useful (0 votes)
5 views15 pages

C++ Design Patterns Overview

Module 3 covers design patterns in C++, which are proven solutions for common software design problems that promote reusable and maintainable architecture. It classifies patterns into three categories: Creational, Structural, and Behavioral, with examples such as Factory, Singleton, and MVC. The module emphasizes that design patterns enhance reusability, flexibility, and scalability in software development.

Uploaded by

23001003104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

C++ Design Patterns Overview

Module 3 covers design patterns in C++, which are proven solutions for common software design problems that promote reusable and maintainable architecture. It classifies patterns into three categories: Creational, Structural, and Behavioral, with examples such as Factory, Singleton, and MVC. The module emphasizes that design patterns enhance reusability, flexibility, and scalability in software development.

Uploaded by

23001003104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like