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:
2026-10-06 16:26:53 +02:00
co-authored by Qwen 3.8 27B
parent c681ebab2b
commit 6e0e45110a
8 changed files with 39 additions and 28 deletions
+7 -1
View File
@@ -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());
}