Download presentation
Presentation is loading. Please wait.
1
The Template Method By Sinclair Schuller
2
What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points Preserves structural integrity of algorithm
3
Why is it Useful? Allows for the factoring out of common behavior Presents a clean method for implementing algorithms Allows for code reuse
4
How is it Structured? Participants Abstract Class Concrete Class Operations Are Hooks Or Abstract *Diagram: http://www.dofactory.com/Patterns/PatternTemplate.aspx
5
How is it Structured? 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)
6
Example void Shape::Draw() { InitializeBrush(); DrawShape(); //Here is our ‘hook’ ClearBrush(); } *Draw() is the template method
7
Example Continued void Rectangle::DrawShape() { //Rectangle Drawing //Implementation } *The Rectangle subclass of Shape overrides DrawShape()
8
If You Only Remember One Thing… Template methods provide a means to develop a skeleton for an algorithm, making it an ideal and necessary method for code reuse. Any Questions?
9
References http://www.dofactory.com/Patterns/Pat ternTemplate.aspx http://www.dofactory.com/Patterns/Pat ternTemplate.aspx Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson and Vlissides
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.