QtDesignPatterns/observerpattern/subject.h

38 lines
552 B
C++

#include <vector>
#include "interface.h"
class Subject
{
public:
Subject(size_t state) : state(state) {}
size_t getState()
{
return this->state;
}
void setState(size_t state)
{
this->state = state;
notifyAllObservers();
}
void attach(Observer *observer)
{
vector.push_back(observer);
}
void notifyAllObservers()
{
for(auto itor : vector)
{
itor->update();
}
}
private:
std::vector<Observer*> vector;
size_t state;
};