7
Behavioral Pattern: Template Method Chapter 5 – Page 1 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 If a change common to both components becomes necessary, duplicate effort must be expended.

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,

Embed Size (px)

Citation preview

Page 1: 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,

Behavioral Pattern:Template Method

Ch

ap

ter 5

– Pag

e

1

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.

Page 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,

The Template Method Pattern

Ch

ap

ter 5

– Pag

e

2

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.

AbstractClass

TemplateMethod()PrimitiveOperation1()PrimitiveOperation2()

ConcreteClass

PrimitiveOperation1()PrimitiveOperation2()

TemplateMethod(): ... PrimitiveOperation1() ... PrimitiveOperation2() ...

The template method calls primitive operations as well as operations defined in the AbstractClass or those of other objects.

Page 3: 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,

Ch

ap

ter 5

– Pag

e

3Non-Software Example: BuildingsBuilders 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.

Building

FloorPlan()Foundation()Framing()Plumbing()Wiring()FrontStairs()Flags()Tower()

AdministrationBuilding

FrontStairs()Flags()Tower()

MunicipalBuilding

FrontStairs()Flags()

OfficeBuilding

FrontStairs()

Page 4: 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,

Ch

ap

ter 5

– Pag

e

4Software Example: AccountsThe 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.

Account

Start()Allow()End()Withdraw()

AccountPower

Start()Allow()End()MaxLimit()

AccountNormal

Start()Allow()End()MaxLimit()

Page 5: 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,

Ch

ap

ter 5

– Pag

e

5AccountsTemplate MethodC++ Code

#include <iostream>using namespace std;

// Base class// Template classclass 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(); }};

Page 6: 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,

Ch

ap

ter 5

– Pag

e

6// Derived classclass 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 classclass 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);}

Page 7: 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,

Template Method Pattern Advantages

Ch

ap

ter 5

– Pag

e

7

• 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.