Sobes.tech
Junior — Middle

Can you tell me about the most common programming patterns you have used?

sobes.tech AI

Answer from AI

Some of the most common programming patterns often used in C/C++:

  • Singleton — ensures that a class has only one instance and provides a global point of access to it.

  • Factory Method — defines an interface for creating an object but allows subclasses to decide which class to instantiate.

  • Observer — allows objects to subscribe to events of another object and receive notifications of changes.

  • Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.

  • RAII (Resource Acquisition Is Initialization) — a resource management idiom where resources are acquired and released in constructors and destructors of objects.

Example of Singleton in C++:

class Singleton {
private:
    Singleton() {}
public:
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
};