Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Design Patterns
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 46 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++AdvancedLesson 46 of 50CamboFreelanceJune 19, 2026

C++ Design Patterns

Learn three essential C++ design patterns: Singleton for unique instances, Factory for decoupled creation, Observer for event notifications.


Tutorials › C++ › Design Patterns
Advanced12 min readLesson 46 of 50

What are Design Patterns?

Design patterns are proven, reusable solutions to common software design problems. We cover three key patterns: Singleton, Factory, and Observer.

Singleton Pattern

Ensures only one instance of a class exists. Thread-safe using C++11 static initialization:

#include <iostream>
using namespace std;

class Logger {
  private:
    Logger() { cout << "Logger created\n"; }
    Logger(const Logger&) = delete;
    Logger& operator=(const Logger&) = delete;

  public:
    static Logger& getInstance() {
        static Logger instance;   // created once, thread-safe in C++11
        return instance;
    }
    void log(const string& msg) { cout << "[LOG] " << msg << endl; }
};

int main() {
    Logger::getInstance().log("App started");
    Logger::getInstance().log("Processing...");
    return 0;
}

Factory Pattern

Creates objects without specifying the exact class — decouples creation from use:

class Button { public: virtual void render() = 0; virtual ~Button(){} };
class WinButton : public Button { public: void render() override { cout << "[Windows Button]" << endl; } };
class MacButton : public Button { public: void render() override { cout << "[Mac Button]" << endl; } };

class ButtonFactory {
  public:
    static unique_ptr<Button> create(const string& type) {
        if (type == "windows") return make_unique<WinButton>();
        if (type == "mac")     return make_unique<MacButton>();
        throw invalid_argument("Unknown button type");
    }
};

auto btn = ButtonFactory::create("mac");
btn->render();   // [Mac Button]

Observer Pattern

Defines a one-to-many dependency — when one object changes, all dependents are notified:

class Observer { public: virtual void update(int value) = 0; };

class EventSource {
    vector<Observer*> observers;
    int value = 0;
  public:
    void subscribe(Observer* o)   { observers.push_back(o); }
    void setValue(int v) {
        value = v;
        for (auto o : observers) o->update(v);
    }
};

class Display : public Observer {
    string name;
  public:
    Display(string n) : name(n) {}
    void update(int v) override {
        cout << name << " received: " << v << endl;
    }
};

Quiz

  1. What does the Singleton pattern guarantee?

    • A) Fast object creation
    • B> Only one instance of the class exists
    • C) Thread safety by default
    • D> Multiple factory methods
    Answer

    B) Singleton ensures only one instance is ever created.

Summary

  • Singleton — one shared instance, lazy initialization.
  • Factory — decouple object creation from usage.
  • Observer — event-driven notification to multiple subscribers.
← Previous: Multithreading Next: Memory Management →
c++design-patternssingletonfactoryadvanced
PreviousLesson 45: C++ MultithreadingNextLesson 47: C++ Memory Management
Back to All Tutorials