Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE3150_Project/ECObserver.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (45 sloc)
986 Bytes
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
// | |
// ECObserver.h | |
// | |
// | |
// Created by Yufeng Wu on 2/27/20. | |
// | |
// | |
#ifndef ECOBERVER_H | |
#define ECOBERVER_H | |
#include <vector> | |
#include <algorithm> | |
//******************************************** | |
// Observer design pattern: observer interface | |
class ECObserver | |
{ | |
public: | |
virtual ~ECObserver() {} | |
virtual void Update() = 0; | |
}; | |
//******************************************** | |
// Observer design pattern: subject | |
class ECObserverSubject | |
{ | |
public: | |
ECObserverSubject() {} | |
virtual ~ECObserverSubject() {} | |
void Attach( ECObserver *pObs ) | |
{ | |
listObservers.push_back(pObs); | |
} | |
void Detach( ECObserver *pObs ) | |
{ | |
listObservers.erase(std::remove(listObservers.begin(), listObservers.end(), pObs), listObservers.end()); | |
} | |
void Notify() | |
{ | |
for(unsigned int i=0; i<listObservers.size(); ++i) | |
{ | |
listObservers[i]->Update(); | |
} | |
} | |
private: | |
std::vector<ECObserver *> listObservers; | |
}; | |
#endif |