design patterns : command

This commit is contained in:
haerong22
2021-12-13 00:52:46 +09:00
parent 69c62d780d
commit b1ec3f9dd1
13 changed files with 261 additions and 0 deletions

View 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();
}
}

View File

@@ -0,0 +1,8 @@
package command.after;
public interface Command {
void execute();
void undo();
}

View 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;
}
}

View 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();
}
}

View 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();
}
}

View 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;
}
}

View 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();
}
}

View 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();
}
}

View 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();
}
}

View 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();
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}