The Template Method By Sinclair Schuller What is

The Template Method By Sinclair Schuller

What is the Template Method? n n n “Skeleton” definition of an algorithm Allows redefinition of predetermined points Preserves structural integrity of algorithm

Why is it Useful? n n n Allows for the factoring out of common behavior Presents a clean method for implementing algorithms Allows for code reuse

How is it Structured? n Participants n n n Abstract Class Concrete Class Operations Are n Hooks Or n Abstract *Diagram: http: //www. dofactory. com/Patterns/Pattern. Template. aspx

How is it Structured? n n n Template method is implemented with calls to primitive operations Primitive operations either allow full (if abstract) or partial (if hook) control to subclass Keep it simple (minimize number of primitive operations)

Example void Shape: : Draw() { Initialize. Brush(); Draw. Shape(); //Here is our ‘hook’ Clear. Brush(); } *Draw() is the template method

Example Continued void Rectangle: : Draw. Shape() { //Rectangle Drawing //Implementation } *The Rectangle subclass of Shape overrides Draw. Shape()

If You Only Remember One Thing… n n Template methods provide a means to develop a skeleton for an algorithm, making it an ideal and necessary method for code reuse. Any Questions?

References n n http: //www. dofactory. com/Patterns/Pat tern. Template. aspx Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson and Vlissides
- Slides: 9