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:
2026-10-07 11:31:16 +02:00
co-authored by Qwen 3.8 27B
parent 11b96e4410
commit a65e84e796
7 changed files with 277 additions and 244 deletions
+12 -10
View File
@@ -1,10 +1,12 @@
#ifndef OBSERVER_H #ifndef OBSERVER_H
#define OBSERVER_H #define OBSERVER_H
class Observer { class Hint;
public:
virtual void update() = 0; class Observer {
}; public:
virtual void update(const Hint&) = 0;
#endif };
#endif
+57 -55
View File
@@ -1,55 +1,57 @@
#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) {
myTimer = new PelicanTimer(*this, &Pelican::timerExpired); 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);
}
void Pelican::press(void) {
if (state == GREEN_IDLE) { void Pelican::press(void) {
state = YELLOW_STATE; if (state == GREEN_IDLE) {
myTimer->timeoutRequest(YELLOW_TIME); state = YELLOW_STATE;
pressed = true; myTimer->timeoutRequest(YELLOW_TIME);
notify(); pressed = true;
} 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) {
switch (state) { void Pelican::timerExpired(void) {
case RED_STATE: switch (state) {
state = YELLOW_FLASHING; case RED_STATE:
myTimer->timeoutRequest(FLASH_TIME); state = YELLOW_FLASHING;
break; myTimer->timeoutRequest(FLASH_TIME);
break;
case YELLOW_FLASHING:
state = GREEN_TIMING; case YELLOW_FLASHING:
myTimer->timeoutRequest(GREEN_TIME); state = GREEN_TIMING;
break; myTimer->timeoutRequest(GREEN_TIME);
break;
case GREEN_TIMING:
if (pressed) { case GREEN_TIMING:
state = YELLOW_STATE; if (pressed) {
myTimer->timeoutRequest(YELLOW_TIME); state = YELLOW_STATE;
} else { myTimer->timeoutRequest(YELLOW_TIME);
state = GREEN_IDLE; } else {
} state = GREEN_IDLE;
break; }
break;
case GREEN_IDLE:
// should never happen case GREEN_IDLE:
break; // should never happen
break;
case YELLOW_STATE:
state = RED_STATE; case YELLOW_STATE:
pressed = false; state = RED_STATE;
myTimer->timeoutRequest(RED_TIME); pressed = false;
break; myTimer->timeoutRequest(RED_TIME);
} break;
}
notify();
} notify(LightHint(state));
notify(WaitHint(pressed));
}
+63 -51
View File
@@ -1,51 +1,63 @@
// ====================================================== // ======================================================
// //
// Represents a Pelican crossing // Represents a Pelican crossing
// //
// ====================================================== // ======================================================
#ifndef PELICAN_H #ifndef PELICAN_H
#define PELICAN_H #define PELICAN_H
#include "Subject.h" #include "Subject.h"
class PelicanTimer; class PelicanTimer;
class Pelican : public Subject { class Pelican : public Subject {
public: public:
Pelican(); Pelican();
// call when the WAIT button has been pressed // call when the WAIT button has been pressed
void press(void); void press(void);
// states that a Pelican can be in // states that a Pelican can be in
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE }; enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
bool isPressed() const { return pressed; } bool isPressed() const { return pressed; }
PelicanState getState() const { return state; } PelicanState getState() const { return state; }
private: private:
// state that this Pelican is in // state that this Pelican is in
PelicanState state; PelicanState state;
// called by PelicanTimer when the lights need to change // called by PelicanTimer when the lights need to change
void timerExpired(void); void timerExpired(void);
const unsigned int RED_TIME; const unsigned int RED_TIME;
const unsigned int YELLOW_TIME; const unsigned int YELLOW_TIME;
const unsigned int GREEN_TIME; const unsigned int GREEN_TIME;
const unsigned int FLASH_TIME; const unsigned int FLASH_TIME;
// indicates whether the WAIT light should be on // indicates whether the WAIT light should be on
bool pressed; bool pressed;
// Timer that indicates when the lights should change // Timer that indicates when the lights should change
PelicanTimer *myTimer; PelicanTimer *myTimer;
private: private:
// prevent general use of copy ctor and operator= // prevent general use of copy ctor and operator=
Pelican(const Pelican &); Pelican(const Pelican &);
Pelican &operator=(const Pelican &); Pelican &operator=(const Pelican &);
}; };
#endif 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
+80 -68
View File
@@ -1,68 +1,80 @@
#include "Pole.h" #include "Pole.h"
#include "Screen.h" #include "Pelican.h"
#include "Screen.h"
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this); #include <typeinfo>
using namespace Screen; // we will discuss namespaces in the course Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this);
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED); using namespace Screen; // we will discuss namespaces in the course
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN); lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN); lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
write(" _____ ", x, ++y, GRAY); lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
write(" /___ /| ", x, ++y, GRAY); lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
write(" | | | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY); write(" _____ ", x, ++y, GRAY);
write(" |____| | ", x, ++y, GRAY); write(" /___ /| ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY); write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY); write(" | |/| ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY); write(" |____| | ", x, ++y, GRAY);
write(" _||__ ", x, ++y, GRAY); write(" | |/| ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY); write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY); write(" |____|/ ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY); write(" _||__ ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY); write(" /___ /| ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" | | | ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" | | | ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" |____|/ ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write("========||===========", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write("_ /_________ /_ ", x, ++y, GRAY); write(" || ", x, ++y, GRAY);
write("/ / ", x, ++y, GRAY); write("========||===========", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY);
update(); write("_ /_________ /_ ", x, ++y, GRAY);
} write("/ / ", x, ++y, GRAY);
Pole::~Pole() { setTrafficLights(myPelican.getState());
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i) setWaitLight(myPelican.isPressed());
delete lights[i]; }
delete waitLight;
} Pole::~Pole() {
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
void Pole::setTrafficLights(Pelican::PelicanState state) { delete lights[i];
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = { delete waitLight;
// red yellow green green man red man }
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash void Pole::setTrafficLights(Pelican::PelicanState state) {
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle // red yellow green green man red man
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow {Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
}; {Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
for (int i = RED_LIGHT; i <= RED_MAN; i++) { {Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
lights[i]->set(LightStates[state][i]); {Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
} };
}
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); } lights[i]->set(LightStates[state][i]);
}
void Pole::update() { }
setTrafficLights(myPelican.getState());
setWaitLight(myPelican.isPressed()); void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
}
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);
}
}
+28 -28
View File
@@ -1,28 +1,28 @@
#ifndef POLE_H #ifndef POLE_H
#define POLE_H #define POLE_H
#include "Light.h" #include "Light.h"
#include "Observer.h" #include "Observer.h"
#include "Pelican.h" #include "Pelican.h"
class Pole : public Observer { class Pole : public Observer {
public: public:
Pole(int x, int y, Pelican &p); Pole(int x, int y, Pelican &p);
~Pole(); ~Pole();
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;
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN }; enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
Light *lights[RED_MAN + 1]; Light *lights[RED_MAN + 1];
Light *waitLight; Light *waitLight;
}; };
#endif #endif
+12 -12
View File
@@ -1,12 +1,12 @@
#include "Subject.h" #include "Subject.h"
#include "Observer.h" #include "Observer.h"
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);
} }
} }
+25 -20
View File
@@ -1,20 +1,25 @@
#ifndef SUBJECT_H #ifndef SUBJECT_H
#define SUBJECT_H #define SUBJECT_H
class Observer; class Observer;
#include <set> #include <set>
class Subject { class Hint {
public: public:
typedef std::set<Observer *> ObsList; virtual ~Hint() = default;
void attach(Observer &obs); };
void notify() const;
class Subject {
private: public:
ObsList myObservers; typedef std::set<Observer *> ObsList;
}; void attach(Observer &obs);
void notify(const Hint&) const;
#endif
private:
ObsList myObservers;
};
#endif