Template method pattern: Difference between revisions

Content deleted Content added
m Cleaning up a randomly generated list using AWB
Line 1:
{{multiple issues|
{{more footnotes|date=March 2012}}
{{expert needed|date=May 2016}}
}}
In [[software engineering]], the '''template method pattern''' is a [[behavioral pattern|behavioral]] [[software design pattern|design pattern]] that defines the [[program skeleton]] of an [[algorithm]] in an operation, deferring some steps to [[Subclass (computer science)|subclass]]es.<ref name=":0">{{cite book
Line 22:
| publisher = Addison-Wesley
| isbn = 0-201-63361-2
}}</ref> It lets one redefine certain steps of an algorithm without changing the algorithm's structure.<ref name=":2">{{cite book|url=http://shop.oreilly.com/product/9780596007126.do|title=Head First Design Patterns|last2=Freeman|first2=Elisabeth|last3=Sierra|first3=Kathy|last4=Bates|first4=Bert|publisher=O'REILLY|year=2004|isbn=978-0-596-00712-6|editor-last=Hendrickson|editor-first=Mike|volume=1|___location=|pagepages=289, 311|pages=|format=paperback|quote=|editor-last2=Loukides|editor-first2=Mike|via=|last1=Freeman|first1=Eric|accessdate=2012-09-12}}</ref>
 
==Overview==
Line 38:
Usually, subclasses control how the behavior of a parent class is redefined, and they aren't restricted to redefine only certain parts of a behavior.
 
<big>What solution does the Template Method design pattern describe?</big>
 
Define abstract operations (''primitives'') for the variant parts of a behavior.
Line 54:
 
==Introduction==
In the template method of this design pattern, one or more algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed.<ref name=":0" />
 
In object-oriented programming, a concrete class is created that provides the steps of an [[algorithm design]]. Steps that are considered invariant are implemented inside the base class. The steps that are considered to be variant, are given a default implementation or none at all. These variant steps must be supplied by concrete derived subclasses.<ref name=":1" /> Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses.
Line 66:
== Structure ==
=== UML class diagram ===
[[File:w3sDesign Template Method Design Pattern UML.jpg|frame|none|A sample UML class diagram for the Template Method design pattern. <ref>{{cite web|title=The Template Method design pattern - Structure |url=http://w3sdesign.com/?gr=b10&ugr=struct|website=w3sDesign.com|accessdate=2017-08-12}}</ref>]]
 
In the above [[Unified Modeling Language|UML]] [[Class diagram|class diagram]], the <code>AbstractClass</code> defines a <code>templateMethod()</code> operation that
defines the skeleton (template) of a behavior by
* implementing the invariant parts of the behavior and
* calling abstract <code>primitive1()</code> and <code>primitive2()</code> operations to defer implementing the variant parts to <code>SubClass1</code>.<br>
 
=== Class diagram ===
Line 110:
* [http://www.24bytes.com/2011/10/template-method.html Template Method Example]
{{Design Patterns Patterns}}
 
 
 
<!--Interwikies-->