Created
March 3, 2022 12:57
-
-
Save yknishidate/e8a54b7455294963446f989ccc1524d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
enum class Event | |
{ | |
EntityFell, | |
}; | |
class Observer | |
{ | |
public: | |
virtual ~Observer() {} | |
virtual void onNotify(Event event) = 0; | |
}; | |
class Achievements : public Observer | |
{ | |
public: | |
void onNotify(Event event) override | |
{ | |
switch(event){ | |
case Event::EntityFell: | |
std::cout << "Achievement unlocked!" << std::endl; | |
break; | |
} | |
} | |
}; | |
class Subject | |
{ | |
public: | |
void addObserver(Observer* observer) | |
{ | |
observers.push_back(observer); | |
} | |
protected: | |
void notify(Event event) | |
{ | |
for(auto& observer: observers){ | |
observer->onNotify(event); | |
} | |
} | |
private: | |
std::vector<Observer*> observers; | |
}; | |
class Physics : public Subject | |
{ | |
public: | |
void update() | |
{ | |
// 通知を送信 | |
// Achievementsと結合していない | |
notify(Event::EntityFell); | |
} | |
}; | |
int main() | |
{ | |
Achievements achievements; | |
Physics physics; | |
physics.addObserver(&achievements); | |
physics.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Game Programming Pattersではさらに色々言及されてる