1 / 7

Behavioral Pattern: Template Method

Behavioral Pattern: Template Method. Chapter 5 – Page 217. On occasion, two different components in a software system will have significant similarities, but will demonstrate no reuse of common interface or implementation .

perry
Download Presentation

Behavioral Pattern: Template Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Behavioral Pattern: Template Method Chapter 5 – Page 217 On occasion, two different components in a software system will have significant similarities, but will demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended. 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.

  2. The Template Method Pattern Chapter 5 – Page 218 • The AbstractClassdefines 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 template method calls primitive operations as well as operations defined in the AbstractClass or those of other objects. • The ConcreteClassimplements the primitive operations to carry out subclass-specific steps of the algorithm.

  3. Non-Software Example: Buildings Chapter 5 – Page 219 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.

  4. Software Example: Accounts Chapter 5 – Page 220 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.

  5. Accounts Template Method C++ Code Chapter 5 – Page 221 #include <iostream> 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 intMaxLimit() = 0; // Template Method voidWithdraw(int amount) { Start(); cout<< "Withdraw Request: $" << amount << endl; intlimit = MaxLimit(); if( amount <= limit ) { cout<< "(Within $" << limit << " Limit)" << endl; Allow(); } else cout<< "Not Allowed: Over $" << limit << " Limit" << endl; End(); } };

  6. Chapter 5 – Page 222 // Derived class classAccountNormal : public Account { public: voidStart() { cout << "Normal Start ..." << endl; } voidAllow() { cout << "Normal Allow ..." << endl; } voidEnd() { cout << "Normal End ..." << endl << endl; } intMaxLimit() { return 1000; } }; // Derived class classAccountPower : public Account { public: voidStart() { cout << "Power Start ..." << endl; } voidAllow() { cout << "Power Allow ..." << endl; } voidEnd() { cout << "Power End ..." << endl << endl; } intMaxLimit() { return 5000; } }; void main() { AccountPowerpower; power.Withdraw(1500); AccountNormalnormal; normal.Withdraw(1500); }

  7. Template Method Pattern Advantages Chapter 5 – Page 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.

More Related