Template method pattern: Difference between revisions

Content deleted Content added
fixing typo
Line 4:
{{expert needed|date=May 2016}}
}}
 
In [[object-oriented programming]], the '''template method''' is one of the [[behavioral pattern|behavioral]] [[software design pattern|design pattern]]<nowiki/>s identified by Gamma et al.<ref name=":0">{{cite book|title=Design Patterns|last1=Gamma|first1=Erich|last2=Helm|first2=Richard|last3=Johnson|first3=Ralph|last4=Vlissides|first4=John|publisher=Addison-Wesley|year=1994|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/325 325–330]|chapter=Template Method|authorlink1=Erich Gamma|authorlink2=Richard Helm|authorlink3=Ralph Johnson (computer scientist)|authorlink4=John Vlissides|title-link=Design Patterns}}</ref> in the book ''[[Design Patterns]]''. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional ''helper methods'' in the same class as the template method.
 
Line 20 ⟶ 21:
 
Some of the self messages sent by the template method may be to ''hook methods.'' These methods are implemented in the same base class as the template method, but with empty bodies (i.e., they do nothing). Hook methods exist so that subclasses can override them, and can thus fine-tune the action of the algorithm ''without'' the need to override the template method itself. In other words, they provide a "hook" on which to "hang" variant implementations.
 
=== Class diagram ===
<br />
 
== Structure ==
Line 36 ⟶ 34:
==Usage==
 
The template method is used in frameworks, where each implements the invariant parts of a ___domain's architecture, while providing hook methods for customization. This is an example of [[inversion of control]]. The template method is used for the following reasons.<ref name=":1">{{cite web | url = http://sourcemaking.com/design_patterns/template_method | title = Template Method Design Pattern | publisher = Source Making - teaching IT professional | quote = Template Method is used prominently in frameworks. | accessdate = 2012-09-12}}</ref>
* It lets subclasses implement varying behavior (through [[Method overriding (programming)|overriding]] of the hook methods).<ref name=":3">{{Cite book|title=Pro Objective-C Design Patterns for iOS|last=Chung|first=Carlo|publisher=Apress|year=2011|isbn=978-1-4302-3331-2|___location=BerkelyBerkeley, CA|pages=266|quote=|via=}}</ref>
| url = http://sourcemaking.com/design_patterns/template_method
| title = Template Method Design Pattern
| publisher = Source Making - teaching IT professional
| quote = Template Method is used prominently in frameworks.
| accessdate = 2012-09-12
}}</ref>
* It lets subclasses implement varying behavior (through [[Method overriding (programming)|overriding]] of the hook methods).<ref name=":3">{{Cite book|title=Pro Objective-C Design Patterns for iOS|last=Chung|first=Carlo|publisher=Apress|year=2011|isbn=978-1-4302-3331-2|___location=Berkely, CA|pages=266|quote=|via=}}</ref>
* It avoids duplication in the code: the general workflow of the algorithm is implemented once in the abstract class's template method, and necessary variations are implemented in the subclasses.<ref name=":3" />
* It controls the point(s) at which specialization is permitted. If the subclasses were to simply override the template method, they could make radical and arbitrary changes to the workflow. In contrast, by overriding only the hook methods, only certain specific details of the workflow can be changed,<ref name=":3" /> and the overall workflow is left intact.
Line 68 ⟶ 60:
* [http://sourcemaking.com/design_patterns/template_method Template Method Design Pattern]
 
<!--Interwikies-->
 
<!--Categories-->
[[Category:Software design patterns]]
[[Category:Articles with example Java code]]