Download presentation
Presentation is loading. Please wait.
Published bySheila Miles Modified over 9 years ago
2
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities, but will demonstrate no reuse of common interface or implementation. The Template Method Pattern handles this problem by having the skeleton of an algorithm defined in a base class, with placeholders replacing certain key steps. Derived classes then implement the placeholders, allowing those steps of the algorithm to be altered without modifying the algorithm’s basic structure. If a change common to both components becomes necessary, duplicate effort must be expended.
3
The Template Method Pattern C h a p t e r 5 – P a g e 218 The AbstractClass defines abstract primitive operations that concrete subclasses define to implement the steps of an algorithm, and implements a template method defining the skeleton of an algorithm. The ConcreteClass implements the primitive operations to carry out subclass-specific steps of the algorithm. The template method calls primitive operations as well as operations defined in the AbstractClass or those of other objects.
4
C h a p t e r 5 – P a g e 219 Non-Software Example: Buildings Builders use the Template Method when developing a new subdivision or office complex. A typical subdivision consists of a limited number of floor plans with different variations available for each. Within a floor plan, the foundation, framing, plumbing, and wiring will be identical for each house. Variation is introduced in the later stages of construction to produce a wider variety of models.
5
C h a p t e r 5 – P a g e 220 Software Example: Accounts The abstract Account class contains the template method Withdraw, as well as placeholders for the primitive methods Start(), Allow(), and End(). Each of the derived classes contain their own implementations for the primitive operations. Essentially, the Account will use the MaxLimit value associated with each derived class to determine whether a specific withdrawal will be allowed.
6
C h a p t e r 5 – P a g e 221 Accounts Template Method C++ Code #include using namespace std; // Base class // Template class class Account { public: // Abstract Methods virtual void Start() = 0; virtual void Allow() = 0; virtual void End() = 0; virtual int MaxLimit() = 0; // Template Method void Withdraw(int amount) { Start(); cout << "Withdraw Request: $" << amount << endl; int limit = MaxLimit(); if ( amount <= limit ) { cout << "(Within $" << limit << " Limit)" << endl; Allow(); } else cout << "Not Allowed: Over $" << limit << " Limit" << endl; End(); } };
7
C h a p t e r 5 – P a g e 222 // Derived class class AccountNormal : public Account { public: void Start() { cout << "Normal Start..." << endl; } void Allow() { cout << "Normal Allow..." << endl; } void End() { cout << "Normal End..." << endl << endl; } int MaxLimit() { return 1000; } }; // Derived class class AccountPower : public Account { public: void Start() { cout << "Power Start..." << endl; } void Allow() { cout << "Power Allow..." << endl; } void End() { cout << "Power End..." << endl << endl; } int MaxLimit() { return 5000; } }; void main() { AccountPower power; power.Withdraw(1500); AccountNormal normal; normal.Withdraw(1500); }
8
Template Method Pattern Advantages C h a p t e r 5 – P a g e 223 The primary advantage of the Template Method Pattern is the elimination of code duplication. In general, composition is favored over inheritance, primarily because of composition’s tendency not to break encapsulation. The Template Method Pattern, however, favors inheritance, since it achieves greater flexibility by allowing subclasses to use some operations of the superclass while overriding others. In addition, this pattern takes advantage of polymorphism, allowing the base class to automatically call the methods of the correct subclasses.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.