Grammar fixes, template method

This commit is contained in:
Ilkka Seppälä 2021-06-22 20:30:39 +03:00
parent 808df54dc0
commit 5b0f4a9697
No known key found for this signature in database
GPG Key ID: 31B7C8F5CC412ECB

View File

@ -17,10 +17,11 @@ structure.
## Explanation
Real world example
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
@ -117,7 +118,7 @@ public class HalflingThief {
}
```
And finally we show how the halfling thief utilizes the different stealing methods.
And finally, we show how the halfling thief utilizes the different stealing methods.
```java
var thief = new HalflingThief(new HitAndRunMethod());
@ -135,14 +136,14 @@ And finally we show how the halfling thief utilizes the different stealing metho
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
* 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
* When common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a 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
* To control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points
## Tutorial
## Tutorials
* [Template-method Pattern Tutorial](https://www.journaldev.com/1763/template-method-design-pattern-in-java)
## Real world examples
## Known uses
* [javax.servlet.GenericServlet.init](https://jakarta.ee/specifications/servlet/4.0/apidocs/javax/servlet/GenericServlet.html#init--):
Method `GenericServlet.init(ServletConfig config)` calls the parameterless method `GenericServlet.init()` which is intended to be overridden in subclasses.