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
+9 -11
View File
@@ -1,11 +1,9 @@
#include "PelicanTimer.h" #include "PelicanTimer.h"
PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {} PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {}
void PelicanTimer::tick() { void PelicanTimer::doExpired() {
if (isExpired()) { (myTarget.*myTargetFunc)();
(myTarget.*myTargetFunc)(); }
}
}
+27 -28
View File
@@ -1,28 +1,27 @@
// ====================================================== // ======================================================
// //
// This class specializes the Timer to enable it to // This class specializes the Timer to enable it to
// stimulate a Pelican // stimulate a Pelican
// //
// ====================================================== // ======================================================
#ifndef PELICANTIMER_H #ifndef PELICANTIMER_H
#define PELICANTIMER_H #define PELICANTIMER_H
#include "Pelican.h" #include "Pelican.h"
#include "Timer.h" #include "Timer.h"
class PelicanTimer : public Timer { class PelicanTimer : public Timer {
public: public:
typedef void (Pelican::*TargetFunc)(); typedef void (Pelican::*TargetFunc)();
PelicanTimer(Pelican &target, TargetFunc targetFunc); PelicanTimer(Pelican &target, TargetFunc targetFunc);
// TODO: Rename this function to doExpired. The parameters and return type are unchanged. void doExpired();
virtual void tick();
private:
private: Pelican &myTarget;
Pelican &myTarget; TargetFunc myTargetFunc;
TargetFunc myTargetFunc; };
};
#endif
#endif
+23 -17
View File
@@ -1,17 +1,23 @@
#include "Timer.h" #include "Timer.h"
#include "TimerSet.h" #include "TimerSet.h"
void Timer::timeoutRequest(int seconds) { timeToGo = seconds; } void Timer::timeoutRequest(int seconds) { timeToGo = seconds; }
Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); } Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); }
bool Timer::isExpired() { void Timer::tick() {
if (timeToGo > 0) { if (isExpired()) {
if (--timeToGo == 0) { doExpired();
return true; }
} }
}
return false; bool Timer::isExpired() {
} if (timeToGo > 0) {
if (--timeToGo == 0) {
return true;
}
}
return false;
}
+30 -27
View File
@@ -1,27 +1,30 @@
// ====================================================== // ======================================================
// //
// A general-purpose countdown timer // A general-purpose countdown timer
// To use this class, specialize it // To use this class, specialize it
// and override tick() // and override tick()
// //
// ====================================================== // ======================================================
#ifndef timer_H #ifndef timer_H
#define timer_H #define timer_H
class Timer { class Timer {
public: public:
Timer(); Timer();
void timeoutRequest(int seconds); void timeoutRequest(int seconds);
virtual void tick() = 0; void tick();
protected: private:
bool isExpired(); virtual void doExpired() = 0;
private: protected:
int timeToGo; bool isExpired();
};
private:
#endif int timeToGo;
};
#endif