feat(cmat): implement Ex41 observer pattern
Refactor the Pelican crossing exercise to use the Observer pattern: Pelican acts as Subject and each Pole observes it, replacing direct calls through the owned Pole pointer. Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
This commit is contained in:
+6
-11
@@ -3,29 +3,25 @@
|
||||
#include "PelicanTimer.h"
|
||||
#include "Pole.h"
|
||||
|
||||
Pelican::Pelican(int x, int y)
|
||||
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), myPole(new Pole(x, y)),
|
||||
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);
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
notify();
|
||||
}
|
||||
|
||||
Pelican::~Pelican() { delete myPole; }
|
||||
Pelican::~Pelican() { }
|
||||
|
||||
void Pelican::press(void) {
|
||||
if (state == GREEN_IDLE) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
}
|
||||
notify();
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
@@ -60,6 +56,5 @@ void Pelican::timerExpired(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
notify();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user