C++ design pattern: Observer

Are there any resources that teach (with examples) the observer pattern and other design patterns?There's this book called Game Programming Patterns, it teaches all the import patterns, but the source code that the author uses in the examples is difficult for a beginner like me.

I tried to write my own implementation of the pattern, but I feel I already made a lot of mistakes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Entity {
public:
    std::string name = "bob";
};

class Event {
    void print() {
        std::cout << "printme" << std::endl;
    }
};

class IObserver {
public:
    virtual ~IObserver();
    virtual void onNotify();
private:
    
};

class Observer {
public:
    void onNotify(Entity &e, Event event) {
        std::cout << e.name << std::endl;
    }
};

class ISubject {
public:
virtual void add() = 0;
virtual void remove() = 0;
private:
IObserver* observers[3];
int numOfObservers;
};

class Subject {
// Didn't implement it yet.
};
Last edited on
https://en.wikipedia.org/wiki/Observer_pattern perhaps? The wiki has all the major ones.
Have a look at Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software by Dmitri Nesteruk https://www.apress.com/gb/book/9781484236024
Topic archived. No new replies allowed.