refactor(Ex22): apply template method to Timer

Rename the virtual tick() override in PelicanTimer to doExpired()
and give the base Timer class a non-virtual tick() that checks for
expiration and dispatches to doExpired(). The exercise starting
point now matches SolutionEx22.

Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
This commit is contained in:
2026-10-05 14:35:16 +02:00
co-authored by Qwen 3.8 27B
parent 73114f1bab
commit db0d7b9bee
4 changed files with 89 additions and 83 deletions
+1 -3
View File
@@ -4,8 +4,6 @@
PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {}
void PelicanTimer::tick() {
if (isExpired()) {
void PelicanTimer::doExpired() {
(myTarget.*myTargetFunc)();
}
}
+1 -2
View File
@@ -17,8 +17,7 @@ class PelicanTimer : public Timer {
PelicanTimer(Pelican &target, TargetFunc targetFunc);
// TODO: Rename this function to doExpired. The parameters and return type are unchanged.
virtual void tick();
void doExpired();
private:
Pelican &myTarget;
+6
View File
@@ -7,6 +7,12 @@ void Timer::timeoutRequest(int seconds) { timeToGo = seconds; }
Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); }
void Timer::tick() {
if (isExpired()) {
doExpired();
}
}
bool Timer::isExpired() {
if (timeToGo > 0) {
if (--timeToGo == 0) {
+4 -1
View File
@@ -15,7 +15,10 @@ class Timer {
void timeoutRequest(int seconds);
virtual void tick() = 0;
void tick();
private:
virtual void doExpired() = 0;
protected:
bool isExpired();