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>
64 lines
1.4 KiB
C++
64 lines
1.4 KiB
C++
// ======================================================
|
|
//
|
|
// Represents a Pelican crossing
|
|
//
|
|
// ======================================================
|
|
|
|
#ifndef PELICAN_H
|
|
#define PELICAN_H
|
|
|
|
#include "Subject.h"
|
|
|
|
class PelicanTimer;
|
|
|
|
class Pelican : public Subject {
|
|
public:
|
|
Pelican();
|
|
|
|
// call when the WAIT button has been pressed
|
|
void press(void);
|
|
|
|
// states that a Pelican can be in
|
|
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
|
|
|
|
bool isPressed() const { return pressed; }
|
|
PelicanState getState() const { return state; }
|
|
|
|
private:
|
|
// state that this Pelican is in
|
|
PelicanState state;
|
|
|
|
// called by PelicanTimer when the lights need to change
|
|
void timerExpired(void);
|
|
|
|
const unsigned int RED_TIME;
|
|
const unsigned int YELLOW_TIME;
|
|
const unsigned int GREEN_TIME;
|
|
const unsigned int FLASH_TIME;
|
|
|
|
// indicates whether the WAIT light should be on
|
|
bool pressed;
|
|
|
|
// Timer that indicates when the lights should change
|
|
PelicanTimer *myTimer;
|
|
|
|
private:
|
|
// prevent general use of copy ctor and operator=
|
|
Pelican(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
|