Update README.md
This commit is contained in:
parent
e6ddff5f25
commit
6cb5b4a683
@ -9,21 +9,30 @@ tags:
|
||||
---
|
||||
|
||||
## Intent
|
||||
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets
|
||||
subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
|
||||
|
||||
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template
|
||||
Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's
|
||||
structure.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> The general steps in stealing an item are the same. First you pick the target, next you confuse him somehow and finally you steal the item. However there are many ways to implement these steps.
|
||||
> The general steps in stealing an item are the same. First you pick the target, next you confuse
|
||||
> him somehow and finally you steal the item. However there are many ways to implement these steps.
|
||||
|
||||
In plain words
|
||||
|
||||
> Template Method pattern outlines the general steps in the parent class and lets the concrete child implementations define the details.
|
||||
> Template Method pattern outlines the general steps in the parent class and lets the concrete child
|
||||
> implementations define the details.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. 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.
|
||||
> In object-oriented programming, the template method is one of the behavioral design patterns
|
||||
> identified by Gamma et al. 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.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
@ -120,9 +129,11 @@ And finally we show how the halfling thief utilizes the different stealing metho
|
||||
```
|
||||
|
||||
## Class diagram
|
||||

|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
The Template Method pattern should be used
|
||||
|
||||
* To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 18 KiB |
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.templatemethod.SubtleMethod" project="template-method"
|
||||
file="/template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="174" x="-90" y="683"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.templatemethod.StealingMethod" project="template-method"
|
||||
file="/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="159" width="177" x="17" y="484"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.templatemethod.HalflingThief" project="template-method"
|
||||
file="/template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="235" x="17" y="320"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.templatemethod.HitAndRunMethod" project="template-method"
|
||||
file="/template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="174" x="124" y="683"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="5">
|
||||
<end type="SOURCE" refId="3" navigable="false">
|
||||
<attribute id="6" name="method"/>
|
||||
<multiplicity id="7" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<generalization id="8">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</generalization>
|
||||
<generalization id="9">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</generalization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
Binary file not shown.
Before Width: | Height: | Size: 34 KiB |
BIN
template-method/etc/template_method_urm.png
Normal file
BIN
template-method/etc/template_method_urm.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
Loading…
x
Reference in New Issue
Block a user