Java 11 migration: patterns (remaining b-c) (#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
6ef840f3cf
commit
33ea7335b1
@ -49,8 +49,8 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
var wizard = new Wizard();
|
||||
var goblin = new Goblin();
|
||||
|
||||
goblin.printStatus();
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class ShrinkSpell extends Command {
|
||||
@Override
|
||||
public void undo() {
|
||||
if (oldSize != null && target != null) {
|
||||
Size temp = target.getSize();
|
||||
var temp = target.getSize();
|
||||
target.setSize(oldSize);
|
||||
oldSize = temp;
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ public class Wizard {
|
||||
*/
|
||||
public void undoLastSpell() {
|
||||
if (!undoStack.isEmpty()) {
|
||||
Command previousSpell = undoStack.pollLast();
|
||||
var previousSpell = undoStack.pollLast();
|
||||
redoStack.offerLast(previousSpell);
|
||||
LOGGER.info("{} undoes {}", this, previousSpell);
|
||||
previousSpell.undo();
|
||||
@ -68,7 +68,7 @@ public class Wizard {
|
||||
*/
|
||||
public void redoLastSpell() {
|
||||
if (!redoStack.isEmpty()) {
|
||||
Command previousSpell = redoStack.pollLast();
|
||||
var previousSpell = redoStack.pollLast();
|
||||
undoStack.offerLast(previousSpell);
|
||||
LOGGER.info("{} redoes {}", this, previousSpell);
|
||||
previousSpell.redo();
|
||||
|
@ -25,15 +25,12 @@ package com.iluwatar.command;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Tests that Command example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,23 +23,23 @@
|
||||
|
||||
package com.iluwatar.command;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
||||
* information needed to perform an action or trigger an event at a later time. This information
|
||||
* includes the method name, the object that owns the method and values for the method parameters.
|
||||
*
|
||||
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
|
||||
* client. A command object (spell) knows about the receiver (target) and invokes a method of
|
||||
* the receiver.Values for parameters of the receiver method are stored in the command. The receiver
|
||||
* then does the work. An invoker object (wizard) knows how to execute a command, and optionally
|
||||
* does bookkeeping about the command execution. The invoker does not know anything about a
|
||||
* concrete command, it knows only about command interface. Both an invoker object and several
|
||||
* command objects are held by a client object (app). The client decides which commands to execute
|
||||
* at which points. To execute a command, it passes the command object to the invoker object.
|
||||
*
|
||||
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
|
||||
* client. A command object (spell) knows about the receiver (target) and invokes a method of the
|
||||
* receiver.Values for parameters of the receiver method are stored in the command. The receiver
|
||||
* then does the work. An invoker object (wizard) knows how to execute a command, and optionally
|
||||
* does bookkeeping about the command execution. The invoker does not know anything about a concrete
|
||||
* command, it knows only about command interface. Both an invoker object and several command
|
||||
* objects are held by a client object (app). The client decides which commands to execute at which
|
||||
* points. To execute a command, it passes the command object to the invoker object.
|
||||
*/
|
||||
public class CommandTest {
|
||||
|
||||
@ -53,8 +53,8 @@ public class CommandTest {
|
||||
@Test
|
||||
public void testCommand() {
|
||||
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
var wizard = new Wizard();
|
||||
var goblin = new Goblin();
|
||||
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
|
||||
@ -79,16 +79,17 @@ public class CommandTest {
|
||||
* This method asserts that the passed goblin object has the name as expectedName, size as
|
||||
* expectedSize and visibility as expectedVisibility.
|
||||
*
|
||||
* @param goblin a goblin object whose state is to be verified against other parameters
|
||||
* @param expectedName expectedName of the goblin
|
||||
* @param expectedSize expected size of the goblin
|
||||
* @param goblin a goblin object whose state is to be verified against other
|
||||
* parameters
|
||||
* @param expectedName expectedName of the goblin
|
||||
* @param expectedSize expected size of the goblin
|
||||
* @param expectedVisibility expected visibility of the goblin
|
||||
*/
|
||||
private void verifyGoblin(Goblin goblin, String expectedName, Size expectedSize,
|
||||
Visibility expectedVisibility) {
|
||||
Visibility expectedVisibility) {
|
||||
assertEquals(expectedName, goblin.toString(), "Goblin's name must be same as expectedName");
|
||||
assertEquals(expectedSize, goblin.getSize(), "Goblin's size must be same as expectedSize");
|
||||
assertEquals(expectedVisibility, goblin.getVisibility(),
|
||||
"Goblin's visibility must be same as expectedVisibility");
|
||||
"Goblin's visibility must be same as expectedVisibility");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user