//C++ 11 or later #include #include // std::chrono::seconds #include #include #include using namespace std; class Observer{ public: virtual void Update(char c)=0; }; class Subject{ public: int SleepInSeconds; int N; char C; Subject(char c,int n,int sleepInSeconds):C(c),N(n),SleepInSeconds(sleepInSeconds){ } virtual void Attach(Observer& observer)=0; virtual bool Detach(Observer& observer)=0; virtual void Notify()=0; }; void EventTriggerer(Subject* ps) {//The thread function for(int i=0;iN;i++){ ps->Notify(); this_thread::sleep_for(chrono::seconds(ps->SleepInSeconds)); } } class CSubject:public Subject{ thread t; vector observers; public: void Attach(Observer& observer){ observers.push_back(&observer); } bool Detach(Observer& observer){ for (auto iter = observers.begin(); iter != observers.end(); iter++) { if (*iter==&observer) { observers.erase(iter); return true; } } return false; } CSubject(char c,int n, int sleepInSeconds):Subject(c,n,sleepInSeconds){ } void StartSimulation(){ t = thread(EventTriggerer, this); } void Join(){ t.join(); } void Notify(){ for (auto &observer : observers) { observer->Update(C); } } }; class CObserver:public Observer{ public: string Name; CObserver(string name):Name(name){ //cout<