mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 06:59:29 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -32,9 +32,9 @@ package com.iluwatar.command;
|
||||
* <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. An invoker object (wizard) receives a reference to the command to be executed and
|
||||
* optionally does bookkeeping about the command execution. The invoker does not know anything
|
||||
* about how the command is executed. The client decides which commands to execute at which
|
||||
* points. To execute a command, it passes a reference of the function to the invoker object.
|
||||
* optionally does bookkeeping about the command execution. The invoker does not know anything about
|
||||
* how the command is executed. The client decides which commands to execute at which points. To
|
||||
* execute a command, it passes a reference of the function to the invoker object.
|
||||
*
|
||||
* <p>In other words, in this example the wizard casts spells on the goblin. The wizard keeps track
|
||||
* of the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
* Goblin is the target of the spells.
|
||||
*/
|
||||
/** Goblin is the target of the spells. */
|
||||
public class Goblin extends Target {
|
||||
|
||||
public Goblin() {
|
||||
|
||||
@@ -26,12 +26,9 @@ package com.iluwatar.command;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Enumeration for target size.
|
||||
*/
|
||||
/** Enumeration for target size. */
|
||||
@RequiredArgsConstructor
|
||||
public enum Size {
|
||||
|
||||
SMALL("small"),
|
||||
NORMAL("normal");
|
||||
|
||||
|
||||
@@ -1,66 +1,58 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Base class for spell targets.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class Target {
|
||||
|
||||
private Size size;
|
||||
|
||||
private Visibility visibility;
|
||||
|
||||
/**
|
||||
* Print status.
|
||||
*/
|
||||
public void printStatus() {
|
||||
LOGGER.info("{}, [size={}] [visibility={}]", this, getSize(), getVisibility());
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the size of the target.
|
||||
*/
|
||||
public void changeSize() {
|
||||
var oldSize = getSize() == Size.NORMAL ? Size.SMALL : Size.NORMAL;
|
||||
setSize(oldSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the visibility of the target.
|
||||
*/
|
||||
public void changeVisibility() {
|
||||
var visible = getVisibility() == Visibility.INVISIBLE
|
||||
? Visibility.VISIBLE : Visibility.INVISIBLE;
|
||||
setVisibility(visible);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/** Base class for spell targets. */
|
||||
@Slf4j
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class Target {
|
||||
|
||||
private Size size;
|
||||
|
||||
private Visibility visibility;
|
||||
|
||||
/** Print status. */
|
||||
public void printStatus() {
|
||||
LOGGER.info("{}, [size={}] [visibility={}]", this, getSize(), getVisibility());
|
||||
}
|
||||
|
||||
/** Changes the size of the target. */
|
||||
public void changeSize() {
|
||||
var oldSize = getSize() == Size.NORMAL ? Size.SMALL : Size.NORMAL;
|
||||
setSize(oldSize);
|
||||
}
|
||||
|
||||
/** Changes the visibility of the target. */
|
||||
public void changeVisibility() {
|
||||
var visible =
|
||||
getVisibility() == Visibility.INVISIBLE ? Visibility.VISIBLE : Visibility.INVISIBLE;
|
||||
setVisibility(visible);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,12 +26,9 @@ package com.iluwatar.command;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Enumeration for target visibility.
|
||||
*/
|
||||
/** Enumeration for target visibility. */
|
||||
@RequiredArgsConstructor
|
||||
public enum Visibility {
|
||||
|
||||
VISIBLE("visible"),
|
||||
INVISIBLE("invisible");
|
||||
|
||||
|
||||
@@ -28,26 +28,20 @@ import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Wizard is the invoker of the commands.
|
||||
*/
|
||||
/** Wizard is the invoker of the commands. */
|
||||
@Slf4j
|
||||
public class Wizard {
|
||||
|
||||
private final Deque<Runnable> undoStack = new LinkedList<>();
|
||||
private final Deque<Runnable> redoStack = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Cast spell.
|
||||
*/
|
||||
/** Cast spell. */
|
||||
public void castSpell(Runnable runnable) {
|
||||
runnable.run();
|
||||
undoStack.offerLast(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo last spell.
|
||||
*/
|
||||
/** Undo last spell. */
|
||||
public void undoLastSpell() {
|
||||
if (!undoStack.isEmpty()) {
|
||||
var previousSpell = undoStack.pollLast();
|
||||
@@ -56,9 +50,7 @@ public class Wizard {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redo last spell.
|
||||
*/
|
||||
/** Redo last spell. */
|
||||
public void redoLastSpell() {
|
||||
if (!redoStack.isEmpty()) {
|
||||
var previousSpell = redoStack.pollLast();
|
||||
|
||||
@@ -24,23 +24,21 @@
|
||||
*/
|
||||
package com.iluwatar.command;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Command example runs without errors.
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Tests that Command example runs without errors. */
|
||||
class AppTest {
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App#main(String[])} throws an exception.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,17 +80,18 @@ 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) {
|
||||
private void verifyGoblin(
|
||||
Goblin goblin, String expectedName, Size expectedSize, 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(),
|
||||
assertEquals(
|
||||
expectedVisibility,
|
||||
goblin.getVisibility(),
|
||||
"Goblin's visibility must be same as expectedVisibility");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user