feat(cmat): pass hints to Ex42 observers
Extend the Ex42 observer pattern with Hint objects so each observer receives the reason for the update instead of querying the subject: - Subject::notify() and Observer::update() now take a const Hint& argument. - Add LightHint and WaitHint carrying the traffic light state and WAIT button state, defined in Pelican.h. - Pole dispatches on hint type using dynamic_cast. - The Pole constructor sets its initial lights directly from the Pelican's current state, since no notification is sent at startup. Stopgap until the initial update is handled properly. Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
#ifndef OBSERVER_H
|
||||
#define OBSERVER_H
|
||||
|
||||
class Hint;
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual void update() = 0;
|
||||
virtual void update(const Hint&) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "Pelican.h"
|
||||
#include "PelicanTimer.h"
|
||||
#include "Subject.h"
|
||||
|
||||
Pelican::Pelican() : pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), state(GREEN_IDLE) {
|
||||
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
|
||||
@@ -12,11 +13,11 @@ void Pelican::press(void) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
notify();
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
notify();
|
||||
}
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
@@ -51,5 +52,6 @@ void Pelican::timerExpired(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
notify();
|
||||
notify(LightHint(state));
|
||||
notify(WaitHint(pressed));
|
||||
}
|
||||
|
||||
@@ -48,4 +48,16 @@ class Pelican : public Subject {
|
||||
Pelican &operator=(const Pelican &);
|
||||
};
|
||||
|
||||
class LightHint : public Hint {
|
||||
public:
|
||||
LightHint(Pelican::PelicanState s) : state(s) { }
|
||||
Pelican::PelicanState state;
|
||||
};
|
||||
|
||||
class WaitHint : public Hint {
|
||||
public:
|
||||
WaitHint (bool p) : pressed(p) {}
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+16
-4
@@ -1,6 +1,9 @@
|
||||
#include "Pole.h"
|
||||
#include "Pelican.h"
|
||||
#include "Screen.h"
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
|
||||
myPelican.attach(*this);
|
||||
|
||||
@@ -36,7 +39,8 @@ Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12
|
||||
write("_ /_________ /_ ", x, ++y, GRAY);
|
||||
write("/ / ", x, ++y, GRAY);
|
||||
|
||||
update();
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
}
|
||||
|
||||
Pole::~Pole() {
|
||||
@@ -62,7 +66,15 @@ void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
void Pole::update() {
|
||||
setTrafficLights(myPelican.getState());
|
||||
setWaitLight(myPelican.isPressed());
|
||||
void Pole::update(const Hint& hint) {
|
||||
const WaitHint* waitHintPtr;
|
||||
waitHintPtr = dynamic_cast<const WaitHint*>(&hint);
|
||||
const LightHint* lightHintPtr;
|
||||
lightHintPtr = dynamic_cast<const LightHint*>(&hint);
|
||||
if (waitHintPtr != nullptr) {
|
||||
setWaitLight(waitHintPtr->pressed);
|
||||
}
|
||||
if (lightHintPtr != nullptr) {
|
||||
setTrafficLights(lightHintPtr->state);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ class Pole : public Observer {
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
void update();
|
||||
void update(const Hint& hint);
|
||||
|
||||
private:
|
||||
Pelican &myPelican;
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
|
||||
|
||||
void Subject::notify() const {
|
||||
void Subject::notify(const Hint& hint) const {
|
||||
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
|
||||
(*iter)->update();
|
||||
(*iter)->update(hint);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-1
@@ -7,11 +7,16 @@ class Observer;
|
||||
|
||||
#include <set>
|
||||
|
||||
class Hint {
|
||||
public:
|
||||
virtual ~Hint() = default;
|
||||
};
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
typedef std::set<Observer *> ObsList;
|
||||
void attach(Observer &obs);
|
||||
void notify() const;
|
||||
void notify(const Hint&) const;
|
||||
|
||||
private:
|
||||
ObsList myObservers;
|
||||
|
||||
Reference in New Issue
Block a user