Post

Behavioral_Command Pattern

✅ Command Pattern

  • encapsulate the request
  • decouple invoker(호출자) and reciever(수신자)
  • 👍🏻 when request logic changes, invoker code will not change

✅ Diagram

Screenshot-2026-02-27-at-08-14-45.png

👀 Command pattern example

✔️ Invoker class

  • the invoker does not know the logic behind the commands
  • even if command changes, just have to press the button (invoke)
  • and the changed logic will run
  • without having to change invoker code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Invoker code will never change even if command changes
public class Button {
    private Command command;

    public Button(Command command) {
        this.command = command;
    }
    public void press(){
        command.execute();
    }

    // main class
    public static void main(String[] args) {
        Button button1 = new Button(new LightsOnCommand(new Light()));
        button1.press();

        Button button2 = new Button(new GameStartCommand(new Game()));
        button2.press();

    }
}

✔️ Command interface

1
2
3
public interface Command {
    void execute();
}

✔️ concrte command interfaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class LightsOnCommand implements Command{
    private Light light;

    public LightsOnCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }
}

public class LightsOffCommand implements Command{
    private Light light;

    public LightsOffCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }
}

public class GameStartCommand implements Command {
    private Game game;

    public GameStartCommand(Game game) {
        this.game = game;
    }

    @Override
    public void execute() {
        game.start();
    }
}

public class GameFinishCommand implements Command {
    private Game game;

    public GameFinishCommand(Game game) {
        this.game = game;
    }

    @Override
    public void execute() {
        game.start();
    }
}

✔️ reciever classs

  • has the specific logic to run
  • when command is called
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Light {
    public void on(){
        //specific logic
    }
    public void off (){
        //specific logic
    }

}

public class Game {
    public void start(){
        //specific logic
    }
    public void finish(){
        //specific logic
    }
}

✅ Using stack with command pattern ➡️ undo

  • can stack the commands
  • and call on undo
  • 실행취소 구현하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 button1 = new Button();
        button1.press(new LightsOnCommand(new Light()));
        Button button2 = new Button();
        button2.press(new GameStartCommand(new Game()));

        button1.undo();
        button2.undo();

    }
}

👍🏻👎🏻

  • 👍🏻 can create new command without modifying invoker
  • 👍🏻 even when command logic changes, invoker will not change
  • 👉🏻 Open Closed principle

🛠️

  • Executor: library to create thread to run asynchronized
  • Runnable
  • SimpleJDBCInsert

1️⃣

2️⃣

3️⃣

4️⃣

  • 1️⃣
  • 2️⃣
  • 3️⃣
  • 4️⃣ 👍🏻 👎🏻
1
2
3
⭐️⭐️⭐️ EXAM ⭐️⭐️⭐️
❓
👉🏻
This post is licensed under CC BY 4.0 by the author.