diff --git a/design-pattern/gof/src/command/after/Button.java b/design-pattern/gof/src/command/after/Button.java new file mode 100644 index 00000000..cc1c16f9 --- /dev/null +++ b/design-pattern/gof/src/command/after/Button.java @@ -0,0 +1,28 @@ +package command.after; + +import java.util.Stack; + +public class Button { + + private Stack commands = new Stack<>(); + + public void press(Command command) { + command.execute(); + commands.push(command); + } + + public void undo() { + if (!commands.isEmpty()) { + Command command = commands.pop(); + command.undo(); + } + } + + public static void main(String[] args) { + Button button = new Button(); + button.press(new GameStartCommand(new Game())); + button.press(new LightOnCommand(new Light())); + button.undo(); + button.undo(); + } +} diff --git a/design-pattern/gof/src/command/after/Command.java b/design-pattern/gof/src/command/after/Command.java new file mode 100644 index 00000000..1e4b5202 --- /dev/null +++ b/design-pattern/gof/src/command/after/Command.java @@ -0,0 +1,8 @@ +package command.after; + +public interface Command { + + void execute(); + + void undo(); +} diff --git a/design-pattern/gof/src/command/after/Game.java b/design-pattern/gof/src/command/after/Game.java new file mode 100644 index 00000000..70405a39 --- /dev/null +++ b/design-pattern/gof/src/command/after/Game.java @@ -0,0 +1,20 @@ +package command.after; + +public class Game { + + private boolean isStarted; + + public void start() { + System.out.println("게임을 시작합니다."); + this.isStarted = true; + } + + public void end() { + System.out.println("게임을 종료합니다."); + this.isStarted = false; + } + + public boolean isStarted() { + return isStarted; + } +} diff --git a/design-pattern/gof/src/command/after/GameEndCommand.java b/design-pattern/gof/src/command/after/GameEndCommand.java new file mode 100644 index 00000000..43268ef1 --- /dev/null +++ b/design-pattern/gof/src/command/after/GameEndCommand.java @@ -0,0 +1,20 @@ +package command.after; + +public class GameEndCommand implements Command { + + private Game game; + + public GameEndCommand(Game game) { + this.game = game; + } + + @Override + public void execute() { + game.end(); + } + + @Override + public void undo() { + new GameStartCommand(this.game).execute(); + } +} diff --git a/design-pattern/gof/src/command/after/GameStartCommand.java b/design-pattern/gof/src/command/after/GameStartCommand.java new file mode 100644 index 00000000..c226c839 --- /dev/null +++ b/design-pattern/gof/src/command/after/GameStartCommand.java @@ -0,0 +1,20 @@ +package command.after; + +public class GameStartCommand implements Command { + + private Game game; + + public GameStartCommand(Game game) { + this.game = game; + } + + @Override + public void execute() { + game.start(); + } + + @Override + public void undo() { + new GameEndCommand(this.game).execute(); + } +} diff --git a/design-pattern/gof/src/command/after/Light.java b/design-pattern/gof/src/command/after/Light.java new file mode 100644 index 00000000..ce270130 --- /dev/null +++ b/design-pattern/gof/src/command/after/Light.java @@ -0,0 +1,20 @@ +package command.after; + +public class Light { + + private boolean isOn; + + public void on() { + System.out.println("불을 켭니다."); + this.isOn = true; + } + + public void off() { + System.out.println("불을 끕니다."); + this.isOn = false; + } + + public boolean isOn() { + return this.isOn; + } +} diff --git a/design-pattern/gof/src/command/after/LightOffCommand.java b/design-pattern/gof/src/command/after/LightOffCommand.java new file mode 100644 index 00000000..04c859f8 --- /dev/null +++ b/design-pattern/gof/src/command/after/LightOffCommand.java @@ -0,0 +1,20 @@ +package command.after; + +public class LightOffCommand implements Command { + + private Light light; + + public LightOffCommand(Light light) { + this.light = light; + } + + @Override + public void execute() { + light.off(); + } + + @Override + public void undo() { + new LightOnCommand(this.light).execute(); + } +} diff --git a/design-pattern/gof/src/command/after/LightOnCommand.java b/design-pattern/gof/src/command/after/LightOnCommand.java new file mode 100644 index 00000000..c5a794d3 --- /dev/null +++ b/design-pattern/gof/src/command/after/LightOnCommand.java @@ -0,0 +1,21 @@ +package command.after; + +public class LightOnCommand implements Command { + + private Light light; + + public LightOnCommand(Light light) { + this.light = light; + } + + @Override + public void execute() { + light.on(); + } + + @Override + public void undo() { + new LightOffCommand(this.light).execute(); + } + +} diff --git a/design-pattern/gof/src/command/after/MyApp.java b/design-pattern/gof/src/command/after/MyApp.java new file mode 100644 index 00000000..d09ea810 --- /dev/null +++ b/design-pattern/gof/src/command/after/MyApp.java @@ -0,0 +1,20 @@ +package command.after; + +public class MyApp { + + private Command command; + + public MyApp(Command command) { + this.command = command; + } + + public void press() { + command.execute(); + } + + public static void main(String[] args) { + MyApp myApp = new MyApp(new GameStartCommand(new Game())); + myApp.press(); + myApp.press(); + } +} diff --git a/design-pattern/gof/src/command/before/Button.java b/design-pattern/gof/src/command/before/Button.java new file mode 100644 index 00000000..bc014d95 --- /dev/null +++ b/design-pattern/gof/src/command/before/Button.java @@ -0,0 +1,22 @@ +package command.before; + +public class Button { + + private Light light; + + public Button(Light light) { + this.light = light; + } + + public void press() { + light.off(); + } + + public static void main(String[] args) { + Button button = new Button(new Light()); + button.press(); + button.press(); + button.press(); + button.press(); + } +} diff --git a/design-pattern/gof/src/command/before/Game.java b/design-pattern/gof/src/command/before/Game.java new file mode 100644 index 00000000..926383c0 --- /dev/null +++ b/design-pattern/gof/src/command/before/Game.java @@ -0,0 +1,20 @@ +package command.before; + +public class Game { + + private boolean isStarted; + + public void start() { + System.out.println("게임을 시작합니다."); + this.isStarted = true; + } + + public void end() { + System.out.println("게임을 종료합니다."); + this.isStarted = false; + } + + public boolean isStarted() { + return isStarted; + } +} diff --git a/design-pattern/gof/src/command/before/Light.java b/design-pattern/gof/src/command/before/Light.java new file mode 100644 index 00000000..bbf71f67 --- /dev/null +++ b/design-pattern/gof/src/command/before/Light.java @@ -0,0 +1,20 @@ +package command.before; + +public class Light { + + private boolean isOn; + + public void on() { + System.out.println("불을 켭니다."); + this.isOn = true; + } + + public void off() { + System.out.println("불을 끕니다."); + this.isOn = false; + } + + public boolean isOn() { + return this.isOn; + } +} diff --git a/design-pattern/gof/src/command/before/MyApp.java b/design-pattern/gof/src/command/before/MyApp.java new file mode 100644 index 00000000..387107ca --- /dev/null +++ b/design-pattern/gof/src/command/before/MyApp.java @@ -0,0 +1,22 @@ +package command.before; + +public class MyApp { + + private Game game; + + public MyApp(Game game) { + this.game = game; + } + + public void press() { + game.start(); + } + + public static void main(String[] args) { + Button button = new Button(new Light()); + button.press(); + button.press(); + button.press(); + button.press(); + } +}