design patterns : command
This commit is contained in:
28
design-pattern/gof/src/command/after/Button.java
Normal file
28
design-pattern/gof/src/command/after/Button.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package command.after;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Button {
|
||||
|
||||
private Stack<Command> 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();
|
||||
}
|
||||
}
|
||||
8
design-pattern/gof/src/command/after/Command.java
Normal file
8
design-pattern/gof/src/command/after/Command.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package command.after;
|
||||
|
||||
public interface Command {
|
||||
|
||||
void execute();
|
||||
|
||||
void undo();
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/Game.java
Normal file
20
design-pattern/gof/src/command/after/Game.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/GameEndCommand.java
Normal file
20
design-pattern/gof/src/command/after/GameEndCommand.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/GameStartCommand.java
Normal file
20
design-pattern/gof/src/command/after/GameStartCommand.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/Light.java
Normal file
20
design-pattern/gof/src/command/after/Light.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/LightOffCommand.java
Normal file
20
design-pattern/gof/src/command/after/LightOffCommand.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
21
design-pattern/gof/src/command/after/LightOnCommand.java
Normal file
21
design-pattern/gof/src/command/after/LightOnCommand.java
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
20
design-pattern/gof/src/command/after/MyApp.java
Normal file
20
design-pattern/gof/src/command/after/MyApp.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
22
design-pattern/gof/src/command/before/Button.java
Normal file
22
design-pattern/gof/src/command/before/Button.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/before/Game.java
Normal file
20
design-pattern/gof/src/command/before/Game.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
20
design-pattern/gof/src/command/before/Light.java
Normal file
20
design-pattern/gof/src/command/before/Light.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
22
design-pattern/gof/src/command/before/MyApp.java
Normal file
22
design-pattern/gof/src/command/before/MyApp.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user