From db0d7b9bee96eb138fc369cb06f5a84255e067fe Mon Sep 17 00:00:00 2001 From: portersky Date: Mon, 5 Oct 2026 14:35:16 +0200 Subject: [PATCH] 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 --- cmat/Ex22/PelicanTimer.cpp | 20 ++++++------- cmat/Ex22/PelicanTimer.h | 55 ++++++++++++++++++------------------ cmat/Ex22/Timer.cpp | 40 ++++++++++++++------------ cmat/Ex22/Timer.h | 57 ++++++++++++++++++++------------------ 4 files changed, 89 insertions(+), 83 deletions(-) diff --git a/cmat/Ex22/PelicanTimer.cpp b/cmat/Ex22/PelicanTimer.cpp index b1a8c97..982889d 100644 --- a/cmat/Ex22/PelicanTimer.cpp +++ b/cmat/Ex22/PelicanTimer.cpp @@ -1,11 +1,9 @@ - - -#include "PelicanTimer.h" - -PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {} - -void PelicanTimer::tick() { - if (isExpired()) { - (myTarget.*myTargetFunc)(); - } -} + + +#include "PelicanTimer.h" + +PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {} + +void PelicanTimer::doExpired() { + (myTarget.*myTargetFunc)(); +} diff --git a/cmat/Ex22/PelicanTimer.h b/cmat/Ex22/PelicanTimer.h index 9c17567..de48615 100644 --- a/cmat/Ex22/PelicanTimer.h +++ b/cmat/Ex22/PelicanTimer.h @@ -1,28 +1,27 @@ -// ====================================================== -// -// This class specializes the Timer to enable it to -// stimulate a Pelican -// -// ====================================================== - -#ifndef PELICANTIMER_H -#define PELICANTIMER_H - -#include "Pelican.h" -#include "Timer.h" - -class PelicanTimer : public Timer { - public: - typedef void (Pelican::*TargetFunc)(); - - PelicanTimer(Pelican &target, TargetFunc targetFunc); - - // TODO: Rename this function to doExpired. The parameters and return type are unchanged. - virtual void tick(); - - private: - Pelican &myTarget; - TargetFunc myTargetFunc; -}; - -#endif +// ====================================================== +// +// This class specializes the Timer to enable it to +// stimulate a Pelican +// +// ====================================================== + +#ifndef PELICANTIMER_H +#define PELICANTIMER_H + +#include "Pelican.h" +#include "Timer.h" + +class PelicanTimer : public Timer { + public: + typedef void (Pelican::*TargetFunc)(); + + PelicanTimer(Pelican &target, TargetFunc targetFunc); + + void doExpired(); + + private: + Pelican &myTarget; + TargetFunc myTargetFunc; +}; + +#endif diff --git a/cmat/Ex22/Timer.cpp b/cmat/Ex22/Timer.cpp index 1384258..59a4c16 100644 --- a/cmat/Ex22/Timer.cpp +++ b/cmat/Ex22/Timer.cpp @@ -1,17 +1,23 @@ - - -#include "Timer.h" -#include "TimerSet.h" - -void Timer::timeoutRequest(int seconds) { timeToGo = seconds; } - -Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); } - -bool Timer::isExpired() { - if (timeToGo > 0) { - if (--timeToGo == 0) { - return true; - } - } - return false; -} + + +#include "Timer.h" +#include "TimerSet.h" + +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) { + return true; + } + } + return false; +} diff --git a/cmat/Ex22/Timer.h b/cmat/Ex22/Timer.h index b52947b..742c761 100644 --- a/cmat/Ex22/Timer.h +++ b/cmat/Ex22/Timer.h @@ -1,27 +1,30 @@ -// ====================================================== -// -// A general-purpose countdown timer -// To use this class, specialize it -// and override tick() -// -// ====================================================== - -#ifndef timer_H -#define timer_H - -class Timer { - public: - Timer(); - - void timeoutRequest(int seconds); - - virtual void tick() = 0; - - protected: - bool isExpired(); - - private: - int timeToGo; -}; - -#endif +// ====================================================== +// +// A general-purpose countdown timer +// To use this class, specialize it +// and override tick() +// +// ====================================================== + +#ifndef timer_H +#define timer_H + +class Timer { + public: + Timer(); + + void timeoutRequest(int seconds); + + void tick(); + + private: + virtual void doExpired() = 0; + + protected: + bool isExpired(); + + private: + int timeToGo; +}; + +#endif