mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 10:18:39 +00:00
refactor: Declares the template method as final (Issue 1099) (#2057)
This commit is contained in:
1 parent
8a7c7b6f21
commit
67f01d43aa
4 files changed
+20
-9
No files matched your search
@@ -39,6 +39,10 @@ Wikipedia says
|
|||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
Let's first introduce the template method class along with its concrete implementations.
|
Let's first introduce the template method class along with its concrete implementations.
|
||||||
|
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
|
||||||
@@ -50,7 +54,7 @@ public abstract class StealingMethod {
|
|||||||
|
|
||||||
protected abstract void stealTheItem(String target);
|
protected abstract void stealTheItem(String target);
|
||||||
|
|
||||||
public void steal() {
|
public final void steal() {
|
||||||
var target = pickTarget();
|
var target = pickTarget();
|
||||||
LOGGER.info("The target has been chosen as {}.", target);
|
LOGGER.info("The target has been chosen as {}.", target);
|
||||||
confuseTarget(target);
|
confuseTarget(target);
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-inline</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public abstract class StealingMethod {
|
|||||||
/**
|
/**
|
||||||
* Steal.
|
* Steal.
|
||||||
*/
|
*/
|
||||||
public void steal() {
|
public final void steal() {
|
||||||
var target = pickTarget();
|
var target = pickTarget();
|
||||||
LOGGER.info("The target has been chosen as {}.", target);
|
LOGGER.info("The target has been chosen as {}.", target);
|
||||||
confuseTarget(target);
|
confuseTarget(target);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
|
||||||
@@ -42,11 +42,14 @@ public class HalflingThiefTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testSteal() {
|
void testSteal() {
|
||||||
final var method = mock(StealingMethod.class);
|
final var method = spy(StealingMethod.class);
|
||||||
final var thief = new HalflingThief(method);
|
final var thief = new HalflingThief(method);
|
||||||
|
|
||||||
thief.steal();
|
thief.steal();
|
||||||
verify(method).steal();
|
verify(method).steal();
|
||||||
|
String target = verify(method).pickTarget();
|
||||||
|
verify(method).confuseTarget(target);
|
||||||
|
verify(method).stealTheItem(target);
|
||||||
|
|
||||||
verifyNoMoreInteractions(method);
|
verifyNoMoreInteractions(method);
|
||||||
}
|
}
|
||||||
@@ -56,19 +59,23 @@ public class HalflingThiefTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testChangeMethod() {
|
void testChangeMethod() {
|
||||||
final var initialMethod = mock(StealingMethod.class);
|
final var initialMethod = spy(StealingMethod.class);
|
||||||
final var thief = new HalflingThief(initialMethod);
|
final var thief = new HalflingThief(initialMethod);
|
||||||
|
|
||||||
thief.steal();
|
thief.steal();
|
||||||
verify(initialMethod).steal();
|
verify(initialMethod).steal();
|
||||||
|
String target = verify(initialMethod).pickTarget();
|
||||||
|
verify(initialMethod).confuseTarget(target);
|
||||||
|
verify(initialMethod).stealTheItem(target);
|
||||||
|
|
||||||
final var newMethod = mock(StealingMethod.class);
|
final var newMethod = spy(StealingMethod.class);
|
||||||
thief.changeMethod(newMethod);
|
thief.changeMethod(newMethod);
|
||||||
|
|
||||||
thief.steal();
|
thief.steal();
|
||||||
verify(newMethod).steal();
|
verify(newMethod).steal();
|
||||||
|
String newTarget = verify(newMethod).pickTarget();
|
||||||
|
verify(newMethod).confuseTarget(newTarget);
|
||||||
|
verify(newMethod).stealTheItem(newTarget);
|
||||||
verifyNoMoreInteractions(initialMethod, newMethod);
|
verifyNoMoreInteractions(initialMethod, newMethod);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user