CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2001 Template Method |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 19-Feb-01 |
Template Method
Introduction
Polymorphism
class Account { public: void virtual Transaction(float amount) { balance += amount;} Account(char* customerName, float InitialDeposit = 0); protected: char* name; float balance; } class JuniorAccount : public Account { public: void Transaction(float amount) {// put code here} } class SavingsAccount : public Account { public: void Transaction(float amount) {// put code here} } Account* createNewAccount() { // code to query customer and determine what type of // account to create }; main() { Account* customer; customer = createNewAccount(); customer->Transaction(amount); }
Deferred Methods
class Account { public: void virtual Transaction() = 0; } class JuniorAccount : public Account { public void Transaction() { put code here} }
Template Methods
class Account { public: void Transaction(float amount); void virtual TransactionSubpartA(); void virtual TransactionSubpartB(); void virtual TransactionSubpartC(); } void Account::Transaction(float amount) { TransactionSubpartA(); TransactionSubpartB(); TransactionSubpartC(); // EvenMoreCode; } class JuniorAccount : public Account { public: void virtual TransactionSubpartA(); } class SavingsAccount : public Account { public: void virtual TransactionSubpartC(); } Account* customer; customer = createNewAccount(); customer->Transaction(amount);
Template Method- The Pattern
Intent
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure
Motivation
An application framework with Application and Document classes
Abstract Application class defines the algorithm for opening and reading a document
void Application::OpenDocument (const char* name ) { if (!CanNotOpenDocument (name)) { return; } Document* doc = DoCreateDocument(); if (doc) { _docs->AddDocument( doc); AboutToOpenDocument( doc); Doc->Open(); Doc->DoRead(); } }
Applicability
Template Method pattern should be used:
Structure
Participants
Consequences
This is the most commonly used of the 23 GoF patterns
Important in class libraries
Inverted control structure
import java.awt.*; class HelloApplication extends Frame { public void paint( Graphics display ) { int startX = 30; int startY = 40; display.drawString( "Hello World", startX, startY ); } }
Consequences
Template methods tend to call:
Implementation
Using C++ access control
Implementing a Template Method [1]
Sample Code
Template method is common in lazy initialization[2]
public class Foo { Bar field; public Bar getField() { if (field == null) field = new Bar( 10); return field; } }
What happens when subclass needs to change the default field value?
public Bar getField() { if (field == null) field = defaultField(); return field; } protected Bar defaultField() { return new Bar( 10); }
Now a subclass can just override defaultField()
The same idea works in constructors
public Foo() { field := defaultField(); }
Now a subclass can change the default value of a field by overriding the default value method for that field
Exercises
1. Find the template method in the Java class hierarchy of Frame that calls the paint(Graphics display) method.
3. Find other examples of the template method in Java or Smalltalk.
4. When I did problem one, my IDE did not help much. How useful was your IDE/tools? Does this mean imply that the use of the template method should be a function of tools available in a language?
5. Much of the presentation in this document follows very closely to the presentation in Design Patterns: Elements of Reusable Object-Oriented Software. This seems like a waste of lecture time (and perhaps a violation of copyright laws). How would you suggest covering patterns in class?
6. In Doc 4 slide 20 exercise 1 you were asked to select a project to examine. Find possible uses of the template method in the project. How do the sample uses effect the project?
7. In your project or other assignments this semester, use the method described in the slide " Implementing a Template Method " to implement a template method.
[1] See Design Patterns Smalltalk Companion pp. 363-364. Also see Reusability Through Self-Encapsulation, Ken Auer, Pattern Languages of Programming Design, 1995, pp. 505-516
[2] See http://www.eli.sdsu.edu/courses/spring01/cs683/notes/coding/coding.html#Heading19 or Smalltalk Best Practice Patterns, Kent Beck, Prentice Hall, 1997 pp. 85-86
Copyright ©, All rights reserved.
2001 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 19-Feb-01    Next