Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Template method pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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. | access-date = 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=Berkeley, CA|pages=266}}</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. === Use with code generators === The template pattern is useful when working with auto-generated code. The challenge of working with generated code is that changes to the source code will lead to changes in the generated code; if hand-written modifications have been made to the generated code, these will be lost. How, then, should the generated code be customized? The Template pattern provides a solution. If the generated code follows the template method pattern, the generated code will all be an abstract superclass. Provided that hand-written customizations are confined to a subclass, the code generator can be run again without risk of over-writing these modifications. When used with code generation, this pattern is sometimes referred to as the [[Generation gap (pattern)|generation gap pattern]].<ref>{{cite book|last1=Vlissides|first1=John|title=Pattern Hatching: Design Patterns Applied|date=1998-06-22|publisher=Addison-Wesley Professional|isbn=978-0201432930|pages=85β101|url=http://www.informit.com/store/pattern-hatching-design-patterns-applied-9780201432930}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)