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
@@ -54,10 +54,10 @@ public class App {
goblin.printStatus();
- wizard.castSpell(new ShrinkSpell(), goblin);
+ wizard.castSpell(goblin::changeSize);
goblin.printStatus();
- wizard.castSpell(new InvisibilitySpell(), goblin);
+ wizard.castSpell(goblin::changeVisibility);
goblin.printStatus();
wizard.undoLastSpell();
diff --git a/command/src/main/java/com/iluwatar/command/Command.java b/command/src/main/java/com/iluwatar/command/Command.java
deleted file mode 100644
index 83010f160..000000000
--- a/command/src/main/java/com/iluwatar/command/Command.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * The MIT License
- * Copyright © 2014-2019 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;
-
-/**
- * Interface for Commands.
- */
-public interface Command {
- void execute(Target target);
-
- void undo();
-
- void redo();
-
- String toString();
-}
diff --git a/command/src/main/java/com/iluwatar/command/Goblin.java b/command/src/main/java/com/iluwatar/command/Goblin.java
index 72ddc43b5..791bd94e3 100644
--- a/command/src/main/java/com/iluwatar/command/Goblin.java
+++ b/command/src/main/java/com/iluwatar/command/Goblin.java
@@ -23,11 +23,16 @@
package com.iluwatar.command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Goblin is the target of the spells.
*/
public class Goblin extends Target {
+ private static final Logger LOGGER = LoggerFactory.getLogger(Goblin.class);
+
public Goblin() {
setSize(Size.NORMAL);
setVisibility(Visibility.VISIBLE);
@@ -38,4 +43,20 @@ public class Goblin extends Target {
return "Goblin";
}
+ /**
+ * changeSize.
+ */
+ public void changeSize() {
+ var oldSize = getSize() == Size.NORMAL ? Size.SMALL : Size.NORMAL;
+ setSize(oldSize);
+ }
+
+ /**
+ * changeVisibility.
+ */
+ public void changeVisibility() {
+ var visible = getVisibility() == Visibility.INVISIBLE
+ ? Visibility.VISIBLE : Visibility.INVISIBLE;
+ setVisibility(visible);
+ }
}
diff --git a/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java b/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java
deleted file mode 100644
index 33e053cc2..000000000
--- a/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * The MIT License
- * Copyright © 2014-2019 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;
-
-/**
- * InvisibilitySpell is a concrete command.
- */
-public class InvisibilitySpell implements Command {
-
- private Target target;
-
- @Override
- public void execute(Target target) {
- target.setVisibility(Visibility.INVISIBLE);
- this.target = target;
- }
-
- @Override
- public void undo() {
- if (target != null) {
- target.setVisibility(Visibility.VISIBLE);
- }
- }
-
- @Override
- public void redo() {
- if (target != null) {
- target.setVisibility(Visibility.INVISIBLE);
- }
- }
-
- @Override
- public String toString() {
- return "Invisibility spell";
- }
-}
diff --git a/command/src/main/java/com/iluwatar/command/ShrinkSpell.java b/command/src/main/java/com/iluwatar/command/ShrinkSpell.java
deleted file mode 100644
index 3f21fc7c1..000000000
--- a/command/src/main/java/com/iluwatar/command/ShrinkSpell.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * The MIT License
- * Copyright © 2014-2019 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;
-
-/**
- * ShrinkSpell is a concrete command.
- */
-public class ShrinkSpell implements Command {
-
- private Size oldSize;
- private Target target;
-
- @Override
- public void execute(Target target) {
- oldSize = target.getSize();
- target.setSize(Size.SMALL);
- this.target = target;
- }
-
- @Override
- public void undo() {
- if (oldSize != null && target != null) {
- var temp = target.getSize();
- target.setSize(oldSize);
- oldSize = temp;
- }
- }
-
- @Override
- public void redo() {
- undo();
- }
-
- @Override
- public String toString() {
- return "Shrink spell";
- }
-}
diff --git a/command/src/main/java/com/iluwatar/command/Wizard.java b/command/src/main/java/com/iluwatar/command/Wizard.java
index dd469d3c0..b4568eebf 100644
--- a/command/src/main/java/com/iluwatar/command/Wizard.java
+++ b/command/src/main/java/com/iluwatar/command/Wizard.java
@@ -35,20 +35,18 @@ public class Wizard {
private static final Logger LOGGER = LoggerFactory.getLogger(Wizard.class);
- private final Deque