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
|
#ifndef OBSERVER_H
|
||||||
#define OBSERVER_H
|
#define OBSERVER_H
|
||||||
|
|
||||||
|
class Hint;
|
||||||
|
|
||||||
class Observer {
|
class Observer {
|
||||||
public:
|
public:
|
||||||
virtual void update() = 0;
|
virtual void update(const Hint&) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "Pelican.h"
|
#include "Pelican.h"
|
||||||
#include "PelicanTimer.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) {
|
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);
|
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
|
||||||
@@ -12,11 +13,11 @@ void Pelican::press(void) {
|
|||||||
state = YELLOW_STATE;
|
state = YELLOW_STATE;
|
||||||
myTimer->timeoutRequest(YELLOW_TIME);
|
myTimer->timeoutRequest(YELLOW_TIME);
|
||||||
pressed = true;
|
pressed = true;
|
||||||
notify();
|
|
||||||
} else if (state != RED_STATE) {
|
} else if (state != RED_STATE) {
|
||||||
pressed = true;
|
pressed = true;
|
||||||
notify();
|
|
||||||
}
|
}
|
||||||
|
notify(LightHint(state));
|
||||||
|
notify(WaitHint(pressed));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pelican::timerExpired(void) {
|
void Pelican::timerExpired(void) {
|
||||||
@@ -51,5 +52,6 @@ void Pelican::timerExpired(void) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
notify();
|
notify(LightHint(state));
|
||||||
|
notify(WaitHint(pressed));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,4 +48,16 @@ class Pelican : public Subject {
|
|||||||
Pelican &operator=(const Pelican &);
|
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
|
#endif
|
||||||
|
|||||||
+16
-4
@@ -1,6 +1,9 @@
|
|||||||
#include "Pole.h"
|
#include "Pole.h"
|
||||||
|
#include "Pelican.h"
|
||||||
#include "Screen.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) {
|
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
|
||||||
myPelican.attach(*this);
|
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);
|
||||||
write("/ / ", x, ++y, GRAY);
|
write("/ / ", x, ++y, GRAY);
|
||||||
|
|
||||||
update();
|
setTrafficLights(myPelican.getState());
|
||||||
|
setWaitLight(myPelican.isPressed());
|
||||||
}
|
}
|
||||||
|
|
||||||
Pole::~Pole() {
|
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::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||||
|
|
||||||
void Pole::update() {
|
void Pole::update(const Hint& hint) {
|
||||||
setTrafficLights(myPelican.getState());
|
const WaitHint* waitHintPtr;
|
||||||
setWaitLight(myPelican.isPressed());
|
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 setTrafficLights(Pelican::PelicanState state);
|
||||||
void setWaitLight(bool pressed);
|
void setWaitLight(bool pressed);
|
||||||
|
|
||||||
void update();
|
void update(const Hint& hint);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pelican &myPelican;
|
Pelican &myPelican;
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
|
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) {
|
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>
|
#include <set>
|
||||||
|
|
||||||
|
class Hint {
|
||||||
|
public:
|
||||||
|
virtual ~Hint() = default;
|
||||||
|
};
|
||||||
|
|
||||||
class Subject {
|
class Subject {
|
||||||
public:
|
public:
|
||||||
typedef std::set<Observer *> ObsList;
|
typedef std::set<Observer *> ObsList;
|
||||||
void attach(Observer &obs);
|
void attach(Observer &obs);
|
||||||
void notify() const;
|
void notify(const Hint&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ObsList myObservers;
|
ObsList myObservers;
|
||||||
|
|||||||
Reference in New Issue
Block a user