From 735b44bd928c7eabdc468c028f6aeedc3bc8c5f1 Mon Sep 17 00:00:00 2001 From: Dominik Gruntz Date: Sun, 17 Nov 2019 15:37:32 +0100 Subject: [PATCH 1/3] Declares the template methods as final --- .../main/java/com/iluwatar/templatemethod/StealingMethod.java | 2 +- twin/src/main/java/com/iluwatar/twin/GameItem.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java index bf206f3a582b..1a2b8422eac7 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java @@ -42,7 +42,7 @@ public abstract class StealingMethod { /** * Steal. */ - public void steal() { + public final void steal() { var target = pickTarget(); LOGGER.info("The target has been chosen as {}.", target); confuseTarget(target); diff --git a/twin/src/main/java/com/iluwatar/twin/GameItem.java b/twin/src/main/java/com/iluwatar/twin/GameItem.java index cc9fb0808ceb..134ca04b2a38 100644 --- a/twin/src/main/java/com/iluwatar/twin/GameItem.java +++ b/twin/src/main/java/com/iluwatar/twin/GameItem.java @@ -36,7 +36,7 @@ public abstract class GameItem { /** * Template method, do some common logic before draw. */ - public void draw() { + public final void draw() { LOGGER.info("draw"); doDraw(); } From 2f6a2f1660efe0b683bea08ee97691f9fba0505b Mon Sep 17 00:00:00 2001 From: Dominik Gruntz Date: Sun, 17 Nov 2019 15:57:22 +0100 Subject: [PATCH 2/3] Extends the description of the template method pattern --- template-method/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/template-method/README.md b/template-method/README.md index 5a1402259f4b..22c2ac2c3e65 100644 --- a/template-method/README.md +++ b/template-method/README.md @@ -15,11 +15,12 @@ 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. +To make sure that subclasses don’t override the template method, the template method should be declared `final`. + ![alt text](./etc/template-method_1.png "Template Method") ## 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 * 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 * to control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points @@ -27,6 +28,10 @@ The Template Method pattern should be used ## Tutorial * [Template-method Pattern Tutorial](https://www.journaldev.com/1763/template-method-design-pattern-in-java) -## Credits +## Real world examples +* [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. +Method `GenericServlet.init(ServletConfig config)` is the template method in this example. +## Credits * [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612) From 66327b078d08d6246506b55b18c04e260a01467f Mon Sep 17 00:00:00 2001 From: Dominik Gruntz Date: Sun, 17 Nov 2019 16:12:40 +0100 Subject: [PATCH 3/3] Removes the final declarations (this was done in another branch) --- .../main/java/com/iluwatar/templatemethod/StealingMethod.java | 2 +- twin/src/main/java/com/iluwatar/twin/GameItem.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java index 1a2b8422eac7..bf206f3a582b 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java @@ -42,7 +42,7 @@ public abstract class StealingMethod { /** * Steal. */ - public final void steal() { + public void steal() { var target = pickTarget(); LOGGER.info("The target has been chosen as {}.", target); confuseTarget(target); diff --git a/twin/src/main/java/com/iluwatar/twin/GameItem.java b/twin/src/main/java/com/iluwatar/twin/GameItem.java index 134ca04b2a38..cc9fb0808ceb 100644 --- a/twin/src/main/java/com/iluwatar/twin/GameItem.java +++ b/twin/src/main/java/com/iluwatar/twin/GameItem.java @@ -36,7 +36,7 @@ public abstract class GameItem { /** * Template method, do some common logic before draw. */ - public final void draw() { + public void draw() { LOGGER.info("draw"); doDraw(); }