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:
+7
-1
@@ -1,8 +1,9 @@
|
||||
#include "Pole.h"
|
||||
#include "Screen.h"
|
||||
|
||||
Pole::Pole(int x, int y) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)) {
|
||||
Pole::Pole(int x, int y, Pelican& pelican) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), pelican(pelican) {
|
||||
using namespace Screen; // we will discuss namespaces in the course
|
||||
pelican.attach(this);
|
||||
|
||||
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
|
||||
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
|
||||
@@ -57,3 +58,8 @@ void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
}
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
|
||||
void Pole::update() {
|
||||
setTrafficLights(pelican.getState());
|
||||
setWaitLight(pelican.isPressed());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user