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