Skip to content

Design Patterns

Overview

Design patterns are reusable solutions to commonly occurring problems in software design. They represent best practices evolved over time by experienced software developers. This guide focuses on the 23 classic design patterns introduced by the Gang of Four (GoF), with all examples implemented in C++.

Why Design Patterns?

  • Proven Solutions: Design patterns provide tested, proven development paradigms
  • Code Reusability: Patterns help you write more maintainable and reusable code
  • Common Vocabulary: They provide a standard terminology for developers to communicate
  • Best Practices: Patterns encapsulate expertise and best practices in software design
  • Practical C++ Examples: All patterns include real-world C++ implementations for Raspberry Pi projects

Pattern Categories

The 23 GoF design patterns are organized into three categories:

1. Creational Patterns

Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

Key Patterns:

  • Singleton: Ensures a class has only one instance and provides a global point of access

    Singleton Pattern

  • Factory Method: Defines an interface for creating objects, letting subclasses decide which class to instantiate

    Factory Method Pattern

  • Abstract Factory: Provides an interface for creating families of related objects

    Abstract Factory Pattern

  • Builder: Separates the construction of complex objects from their representation

    Builder Pattern

  • Prototype: Creates new objects by copying existing instances

    Prototype Pattern

When to Use: When you need more flexibility in object creation than basic constructors provide.

2. Structural Patterns

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

Key Patterns:

  • Adapter: Allows incompatible interfaces to work together

    Adapter Pattern

  • Decorator: Adds new functionality to objects dynamically

    Decorator Pattern

  • Facade: Provides a simplified interface to a complex subsystem

    Facade Pattern

  • Proxy: Provides a placeholder or surrogate for another object

    Proxy Pattern

  • Composite: Composes objects into tree structures to represent hierarchies

    Composite Pattern

  • Bridge: Separates abstraction from implementation

    Bridge Pattern

  • Flyweight: Reduces memory usage by sharing common state

    Flyweight Pattern

When to Use: When you need to organize relationships between objects or create flexible class hierarchies.

3. Behavioral Patterns

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.

Key Patterns:

  • Strategy: Defines a family of algorithms and makes them interchangeable

    Strategy Pattern

  • Observer: Defines a one-to-many dependency between objects

    Observer Pattern

  • Template Method: Defines the skeleton of an algorithm, deferring some steps to subclasses

    Template Method Pattern

  • Command: Encapsulates a request as an object

    Command Pattern

  • State: Allows an object to alter its behavior when its internal state changes

    State Pattern

  • Iterator: Provides a way to access elements of a collection sequentially

    Iterator Pattern

  • Mediator: Defines an object that encapsulates how objects interact

    Mediator Pattern

  • Memento: Captures and restores an object's internal state

    Memento Pattern

  • Visitor: Separates algorithms from the objects on which they operate

    Visitor Pattern

When to Use: When you need to define communication patterns between objects or encapsulate behavior.

Learning Path

We recommend learning the patterns in the following order:

  1. Start with Simple Patterns: Begin with Singleton, Strategy, and Observer - these are the most commonly used and easiest to understand

  2. Move to Structural Patterns: Learn Adapter, Decorator, and Facade - these help you understand how to organize code better

  3. Explore Creation Patterns: Factory Method and Abstract Factory - these teach you flexible object creation

  4. Advanced Patterns: Template Method, Command, State - these require understanding of inheritance and polymorphism

  5. Complex Patterns: Visitor, Mediator, Flyweight - these are more specialized and used in specific scenarios

Pattern Implementation

Each pattern in this guide includes:

  • Intent: What problem the pattern solves
  • Motivation: When and why to use it
  • Structure: UML diagram showing the pattern's structure
  • Implementation: C++ code example
  • Use Cases: Real-world scenarios where the pattern applies
  • Related Patterns: How this pattern relates to others

Getting Started

Choose a category from the navigation menu to explore specific patterns. Each pattern page includes detailed explanations, UML diagrams, and practical C++ implementations.

Happy learning!