Convert Figma logo to code with AI

JakubVojvoda logodesign-patterns-cpp

C++ Design Patterns

4,124
935
4,124
2

Top Related Projects

An ultra-simplified explanation to design patterns

Design patterns implemented in Java

A curated list of software and architecture related design patterns.

A collection of design patterns/idioms in Python

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Quick Overview

JakubVojvoda/design-patterns-cpp is a GitHub repository that provides C++ implementations of various design patterns. It serves as a practical resource for developers to understand and apply common design patterns in C++ programming, offering clear and concise examples for each pattern.

Pros

  • Comprehensive collection of design patterns implemented in C++
  • Well-organized structure with separate directories for each pattern
  • Includes a README for each pattern explaining its purpose and usage
  • Code examples are simple and easy to understand

Cons

  • Some implementations may not follow the most modern C++ practices
  • Limited documentation beyond the basic pattern descriptions
  • Lacks unit tests or extensive error handling
  • Not actively maintained (last commit was over 2 years ago as of 2023)

Code Examples

  1. Singleton Pattern:
class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }

    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

private:
    Singleton() {}
};

This code demonstrates the implementation of the Singleton pattern, ensuring only one instance of the class exists.

  1. Observer Pattern:
class Subject {
    vector<Observer*> observers;
public:
    void attach(Observer* observer) {
        observers.push_back(observer);
    }
    void notify() {
        for (Observer* observer : observers) {
            observer->update();
        }
    }
};

This example shows the core of the Observer pattern, where a Subject maintains a list of observers and notifies them of changes.

  1. Factory Method Pattern:
class Creator {
public:
    virtual Product* factoryMethod() = 0;
    void anOperation() {
        Product* p = factoryMethod();
        p->operation();
    }
};

class ConcreteCreator : public Creator {
public:
    Product* factoryMethod() override {
        return new ConcreteProduct();
    }
};

This code illustrates the Factory Method pattern, where a creator class defines an interface for creating an object, but lets subclasses decide which class to instantiate.

Getting Started

To use these design pattern implementations:

  1. Clone the repository:

    git clone https://github.com/JakubVojvoda/design-patterns-cpp.git
    
  2. Navigate to the desired pattern directory:

    cd design-patterns-cpp/[pattern-name]
    
  3. Compile and run the example:

    g++ main.cpp -o pattern_example
    ./pattern_example
    

Note: Make sure you have a C++ compiler installed on your system.

Competitor Comparisons

An ultra-simplified explanation to design patterns

Pros of design-patterns-for-humans

  • Written in plain English, making it more accessible to beginners
  • Covers a wide range of design patterns with real-world examples
  • Includes visual diagrams to illustrate pattern structures

Cons of design-patterns-for-humans

  • Lacks actual code implementations, focusing more on conceptual explanations
  • Not language-specific, which may be less helpful for those seeking C++ examples
  • Less comprehensive in terms of pattern variations and edge cases

Code Comparison

design-patterns-cpp provides concrete C++ implementations:

class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
private:
    Singleton() {}
};

design-patterns-for-humans offers pseudocode-like examples:

class President
    private constructor
    private static instance: President
    
    public static getInstance()
        if (instance == null)
            instance = new President()
        return instance

Both repositories aim to explain design patterns, but they take different approaches. design-patterns-cpp focuses on practical C++ implementations, while design-patterns-for-humans emphasizes conceptual understanding with language-agnostic explanations. The choice between them depends on whether you're looking for C++-specific examples or a more general, beginner-friendly introduction to design patterns.

Design patterns implemented in Java

Pros of java-design-patterns

  • More comprehensive, covering a wider range of design patterns
  • Includes real-world examples and use cases for each pattern
  • Better documentation and explanations for each pattern implementation

Cons of java-design-patterns

  • Larger codebase, which may be overwhelming for beginners
  • Focuses solely on Java, limiting its applicability to other languages
  • May include more complex implementations due to its comprehensive nature

Code Comparison

design-patterns-cpp (Singleton pattern):

class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
private:
    Singleton() {}
};

java-design-patterns (Singleton pattern):

public final class Singleton {
    private static final Singleton INSTANCE = new Singleton();
    private Singleton() {}
    public static Singleton getInstance() {
        return INSTANCE;
    }
}

Both implementations achieve the Singleton pattern, but the Java version uses eager initialization, while the C++ version uses lazy initialization. The Java implementation is thread-safe by default, whereas the C++ version may require additional synchronization in multi-threaded environments.

A curated list of software and architecture related design patterns.

Pros of awesome-design-patterns

  • Comprehensive collection of design pattern resources across multiple programming languages
  • Includes links to articles, tutorials, and external implementations
  • Covers a wider range of design patterns beyond the traditional GoF patterns

Cons of awesome-design-patterns

  • Lacks direct code examples within the repository
  • May require more time to find specific implementations due to the breadth of resources
  • Not focused on a single programming language, which may be less helpful for C++-specific learning

Code Comparison

design-patterns-cpp provides direct C++ implementations, while awesome-design-patterns doesn't include code examples. Here's a sample from design-patterns-cpp:

class AbstractProductA {
public:
  virtual ~AbstractProductA() {}
  virtual std::string UsefulFunctionA() const = 0;
};

awesome-design-patterns doesn't contain code snippets, instead offering links to external resources.

Summary

design-patterns-cpp is ideal for C++ developers seeking ready-to-use implementations, while awesome-design-patterns serves as a comprehensive resource hub for design patterns across various languages and paradigms. The choice between them depends on whether you need specific C++ code examples or a broader understanding of design patterns in general.

A collection of design patterns/idioms in Python

Pros of python-patterns

  • Written in Python, which is known for its readability and simplicity
  • Includes a wider variety of design patterns (30+)
  • Provides more detailed explanations and comments for each pattern

Cons of python-patterns

  • Less structured organization compared to design-patterns-cpp
  • Some implementations may be less idiomatic or optimized for Python

Code Comparison

design-patterns-cpp (Singleton pattern):

class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
private:
    Singleton() {}
};

python-patterns (Singleton pattern):

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super().__call__(*args, **kwargs)
        return cls._instances[cls]

The C++ implementation uses a static instance and private constructor, while the Python version uses a metaclass to control object creation. The Python approach is more flexible but may be less intuitive for developers unfamiliar with metaclasses.

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Pros of javascript-algorithms

  • Covers a wide range of algorithms and data structures
  • Includes detailed explanations and complexity analysis
  • Actively maintained with frequent updates

Cons of javascript-algorithms

  • Focuses only on algorithms, not design patterns
  • May be overwhelming for beginners due to its extensive content
  • JavaScript-specific implementations may not translate directly to other languages

Code Comparison

design-patterns-cpp (Singleton pattern):

class Singleton {
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
private:
    Singleton() {}
};

javascript-algorithms (Binary search):

function binarySearch(sortedArray, seekElement) {
  let startIndex = 0;
  let endIndex = sortedArray.length - 1;
  while (startIndex <= endIndex) {
    const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
    // ... (implementation continues)
  }
}

The design-patterns-cpp repository focuses on implementing various design patterns in C++, providing concise examples for each pattern. On the other hand, javascript-algorithms offers a comprehensive collection of algorithms and data structures implemented in JavaScript, with detailed explanations and complexity analysis. While design-patterns-cpp is more specialized in its scope, javascript-algorithms covers a broader range of computer science concepts, making it a valuable resource for algorithm study and interview preparation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

C++ Design Patterns

Software design patterns are general reusable solutions to problems which occur over and over again in object-oriented design enviroment. It is not a finished design that can be transformed into source code directly, but it is template how to solve the problem. We can classify them by purpose into creational (abstract the instantiation process), structure (how classes and objects are composed to form larger structures) and behavioral patterns (the assignment of responsibilities between objects).

Creational Patterns

Structural Patterns

  • Adapter, interface to an object
  • Bridge, implementation of an object
  • Composite, structure and composition of an object
  • Decorator, responsibilities of an object without subclassing
  • Façade, interface to a subsystem
  • Flyweight, storage costs of objects
  • Proxy, how an object is accessed (its location)

Behavioral Patterns

  • Chain of Responsibility, object that can fulfill a request
  • Command, when and how a request is fulfilled
  • Interpreter, grammar and interpretation of a language
  • Iterator, how an aggregate's elements are accessed
  • Mediator, how and which objects interact with each other
  • Memento, what private information is stored outside an object, and when
  • Observer, how the dependent objects stay up to date
  • State, states of an object
  • Strategy, an algorithm
  • Template Method, steps of an algorithm
  • Visitor, operations that can be applied to objects without changing their classes

Other Languages

In my repository you can find implementation of desgin patterns also in languages as

References

Design patterns in this repository are based on