Menu
Coddy logo textTech

Intro to Design Patterns

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 90 of 104.

Design patterns are proven, reusable solutions to common problems that arise in software design. They aren't code you copy directly - they're templates or blueprints that describe how to structure classes and objects to solve specific challenges elegantly.

The concept was popularized by the "Gang of Four" (GoF) in their 1994 book, which cataloged 23 fundamental patterns. These patterns are typically grouped into three categories:

Category Purpose Examples
Creational Object creation mechanisms Singleton, Factory, Builder
Structural Class and object composition Adapter, Decorator, Composite
Behavioral Object interaction and responsibility Observer, Strategy, Command

Why learn design patterns? They provide a shared vocabulary among developers - saying "use a Factory here" instantly communicates a complex idea. They also help you avoid reinventing solutions and make your code more flexible and maintainable.

In C++, design patterns leverage the OOP features you've already learned: inheritance, polymorphism, abstract classes, and templates. Over the next lessons, you'll implement several essential patterns and understand when each one is the right tool for the job.

challenge icon

Challenge

Easy

Let's build a simple pattern classifier that demonstrates your understanding of the three design pattern categories. You'll create a system that takes a pattern name and identifies which category it belongs to—Creational, Structural, or Behavioral—along with a brief description of that category's purpose.

You'll organize your code across three files:

  • PatternCategory.h: Define an abstract base class called PatternCategory that represents a design pattern category. It should have:
    • A pure virtual method getName() that returns the category name as a string
    • A pure virtual method getPurpose() that returns a description of what patterns in this category do
    • A virtual destructor

    Then create three derived classes that inherit from PatternCategory:

    • CreationalCategory — returns name "Creational" and purpose "Object creation mechanisms"
    • StructuralCategory — returns name "Structural" and purpose "Class and object composition"
    • BehavioralCategory — returns name "Behavioral" and purpose "Object interaction and responsibility"
  • PatternClassifier.h: Create a PatternClassifier class that can identify which category a given pattern belongs to.

    Your classifier should have a method classify that takes a pattern name (string) and returns a pointer to the appropriate PatternCategory. Use smart pointers (std::unique_ptr) for memory management.

    The classifier should recognize these patterns:

    • Creational: Singleton, Factory, Builder
    • Structural: Adapter, Decorator, Composite
    • Behavioral: Observer, Strategy, Command

    If the pattern name isn't recognized, return nullptr.

  • main.cpp: Read a single input—the name of a design pattern.

    Create a PatternClassifier and use it to classify the input pattern. If the pattern is recognized, print:

    [PatternName] is a [CategoryName] pattern
    Purpose: [CategoryPurpose]

    If the pattern isn't recognized, print:

    Unknown pattern: [PatternName]

For example, with input Singleton:

Singleton is a Creational pattern
Purpose: Object creation mechanisms

With input Observer:

Observer is a Behavioral pattern
Purpose: Object interaction and responsibility

With input Proxy:

Unknown pattern: Proxy

This challenge reinforces the three categories of design patterns while practicing inheritance, polymorphism, and smart pointers—all concepts you've already mastered. The multi-file structure mirrors how real projects organize related classes.

Cheat sheet

Design patterns are reusable solutions to common software design problems. They serve as templates for structuring classes and objects, not direct code to copy.

Design patterns are grouped into three main categories:

Category Purpose Examples
Creational Object creation mechanisms Singleton, Factory, Builder
Structural Class and object composition Adapter, Decorator, Composite
Behavioral Object interaction and responsibility Observer, Strategy, Command

Benefits of design patterns:

  • Provide a shared vocabulary among developers
  • Avoid reinventing solutions to common problems
  • Make code more flexible and maintainable

In C++, design patterns leverage OOP features including inheritance, polymorphism, abstract classes, and templates.

Try it yourself

#include <iostream>
#include <string>
#include "PatternClassifier.h"

int main() {
    // Read the pattern name
    std::string patternName;
    std::cin >> patternName;
    
    // TODO: Create a PatternClassifier instance
    
    // TODO: Use the classifier to classify the input pattern
    
    // TODO: If the pattern is recognized, print:
    // "[PatternName] is a [CategoryName] pattern"
    // "Purpose: [CategoryPurpose]"
    
    // TODO: If the pattern is not recognized (nullptr), print:
    // "Unknown pattern: [PatternName]"
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming