mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 16:17:25 +00:00
docs: update template method
This commit is contained in:
1 parent
13d35ec474
commit
8372d925f5
1 file changed
+7
-6
@@ -38,7 +38,6 @@ Our programmatic example is about thieves and stealing. The general steps in ste
|
|||||||
|
|
||||||
Let's first introduce the template method class `StealingMethod` along with its concrete implementations `SubtleMethod` and `HitAndRunMethod`. To make sure that subclasses don’t override the template method, the template method (in our case method `steal`) should be declared `final`, otherwise the skeleton defined in the base class could be overridden in subclasses.
|
Let's first introduce the template method class `StealingMethod` along with its concrete implementations `SubtleMethod` and `HitAndRunMethod`. To make sure that subclasses don’t override the template method, the template method (in our case method `steal`) should be declared `final`, otherwise the skeleton defined in the base class could be overridden in subclasses.
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class StealingMethod {
|
public abstract class StealingMethod {
|
||||||
@@ -56,7 +55,9 @@ public abstract class StealingMethod {
|
|||||||
stealTheItem(target);
|
stealTheItem(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SubtleMethod extends StealingMethod {
|
public class SubtleMethod extends StealingMethod {
|
||||||
|
|
||||||
@@ -75,7 +76,9 @@ public class SubtleMethod extends StealingMethod {
|
|||||||
LOGGER.info("While in close contact grab the {}'s wallet.", target);
|
LOGGER.info("While in close contact grab the {}'s wallet.", target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HitAndRunMethod extends StealingMethod {
|
public class HitAndRunMethod extends StealingMethod {
|
||||||
|
|
||||||
@@ -120,10 +123,12 @@ 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
|
```java
|
||||||
|
public static void main(String[] args) {
|
||||||
var thief = new HalflingThief(new HitAndRunMethod());
|
var thief = new HalflingThief(new HitAndRunMethod());
|
||||||
thief.steal();
|
thief.steal();
|
||||||
thief.changeMethod(new SubtleMethod());
|
thief.changeMethod(new SubtleMethod());
|
||||||
thief.steal();
|
thief.steal();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The program output:
|
The program output:
|
||||||
@@ -137,10 +142,6 @@ The program output:
|
|||||||
11:06:01.723 [main] INFO com.iluwatar.templatemethod.SubtleMethod -- While in close contact grab the shop keeper's wallet.
|
11:06:01.723 [main] INFO com.iluwatar.templatemethod.SubtleMethod -- While in close contact grab the shop keeper's wallet.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
The Template Method pattern should be used
|
The Template Method pattern should be used
|
||||||
@@ -151,7 +152,7 @@ The Template Method pattern should be used
|
|||||||
|
|
||||||
## Tutorials
|
## Tutorials
|
||||||
|
|
||||||
* [Template Method Design Pattern In Java - Digital Ocean](https://www.digitalocean.com/community/tutorials/template-method-design-pattern-in-java)
|
* [Template Method Design Pattern In Java (DigitalOcean)](https://www.digitalocean.com/community/tutorials/template-method-design-pattern-in-java)
|
||||||
|
|
||||||
## Known uses
|
## Known uses
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user