QtDesignPatterns/observerpattern/subject.h

38 lines
552 B
C
Raw Permalink Normal View History

2024-02-06 17:21:37 +08:00
#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;
};