January 2019
Intermediate to advanced
512 pages
14h 5m
English
Another common use of the Template Method is dealing with pre-and post-conditions, or actions. In a class hierarchy, pre-and post-conditions generally verify that the design invariants of the abstraction provided by the interface are not violated by any specific implementation at any point during execution. Such verification naturally submits to the Template Method's design:
class Base { public: void VerifiedAction() { assert(StateIsValid()); ActionImpl(); assert(StateIsValid()); } virtual void ActionImpl() = 0;};class Derived : public Base { public: void ActionImpl() { ... }};
Of course, in software design, one man's invariants are another's customization points. Sometimes, it is the main code that remains ...