Skip to content

Commit 1fbe9bb

Browse files
dgruntziluwatar
authored andcommitted
Changes the description of the template method pattern (as discussed in Issue1099) (iluwatar#1101)
* Declares the template methods as final * Extends the description of the template method pattern * Removes the final declarations (this was done in another branch)
1 parent e0b728c commit 1fbe9bb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

template-method/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@ Define the skeleton of an algorithm in an operation, deferring some
1515
steps to subclasses. Template method lets subclasses redefine certain steps of
1616
an algorithm without changing the algorithm's structure.
1717

18+
To make sure that subclasses don’t override the template method, the template method should be declared `final`.
19+
1820
![alt text](./etc/template-method_1.png "Template Method")
1921

2022
## Applicability
2123
The Template Method pattern should be used
22-
2324
* to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary
2425
* when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is good example of "refactoring to generalize" as described by Opdyke and Johnson. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations
2526
* to control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points
2627

2728
## Tutorial
2829
* [Template-method Pattern Tutorial](https://www.journaldev.com/1763/template-method-design-pattern-in-java)
2930

30-
## Credits
31+
## Real world examples
32+
* [javax.servlet.GenericServlet.init](https://jakarta.ee/specifications/servlet/4.0/apidocs/javax/servlet/GenericServlet.html#init--):
33+
Method `GenericServlet.init(ServletConfig config)` calls the parameterless method `GenericServlet.init()` which is intended to be overridden in subclasses.
34+
Method `GenericServlet.init(ServletConfig config)` is the template method in this example.
3135

36+
## Credits
3237
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)

0 commit comments

Comments
 (0)