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:
@@ -4,8 +4,6 @@
|
||||
|
||||
PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {}
|
||||
|
||||
void PelicanTimer::tick() {
|
||||
if (isExpired()) {
|
||||
void PelicanTimer::doExpired() {
|
||||
(myTarget.*myTargetFunc)();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
@@ -15,7 +15,10 @@ class Timer {
|
||||
|
||||
void timeoutRequest(int seconds);
|
||||
|
||||
virtual void tick() = 0;
|
||||
void tick();
|
||||
|
||||
private:
|
||||
virtual void doExpired() = 0;
|
||||
|
||||
protected:
|
||||
bool isExpired();
|
||||
|
||||
Reference in New Issue
Block a user