Skip to content

Commit faf3761

Browse files
author
minjeongkimi
committedOct 19, 2022
Chapter 6
1 parent a7ab7f9 commit faf3761

13 files changed

+190
-0
lines changed
 

‎Chapter6/.imgs/img.png

140 KB
Loading

‎Chapter6/.imgs/img2.png

72.1 KB
Loading

‎Chapter6/.imgs/img3.png

211 KB
Loading

‎Chapter6/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 커맨드 패턴
2+
3+
만약 만능 리모컨을 만든다면?
4+
![img.png](.imgs/img.png)
5+
![img.png](.imgs/img2.png)
6+
- 연결되어있는 기기들은 공통된 인터페이스가 없고 전부 다르게 생겼다
7+
8+
##기존
9+
- 1번 버튼을 눌렀을 때 TV를 킨다
10+
- 2번 버튼을 눌렀을 때 전등을 킨다
11+
- ...
12+
> 리모컨 기능을 변경하거나 새로운 기기가 추가될 때마다 수정이 크고 변경이 생길 일이 많다
13+
14+
##개선
15+
16+
> 리모컨과 기기 사이에 `커맨드 객체`를 두어서 리모컨이 기기에 대해 알지 못해도 된다
17+
18+
## 예시
19+
1. 고객이 주문서를 작성해 종업원에게 전달 `createOrder()`
20+
2. 종업원이 주문서를 주방에 전달 `takeOrder()`, `orderUp()`
21+
3. 주방장은 주문대로 음식을 준비 `makeBurger()`, `makeShake()`
22+
23+
주방장은 고객에 대해 알지 못해도 주문서대로만 요리를 하면 됨
24+
25+
> - 고객 = 클라이언트 객체
26+
> - 주문서 = 커맨드 객체
27+
> - 종업원 = 인보커 객체
28+
> - `orderUp()` = `execute()`
29+
> - 주방장 = 리시버 객체
30+
> - `takeOrder()` = `setCommand()`

‎Chapter6/src/Client.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Invoker.RemoteControl;
2+
import Receiver.Light;
3+
import Receiver.TV;
4+
5+
public class Client {
6+
public static void main(String[] args) {
7+
8+
TV tv = new TV();
9+
Light light = new Light();
10+
11+
RemoteControl remoteControl = new RemoteControl();
12+
remoteControl.setCommand(0, light::on, light::off);
13+
remoteControl.setCommand(1, tv::wakeUp, tv::sleep);
14+
15+
remoteControl.onButton(0);
16+
remoteControl.offButton(1);
17+
18+
}
19+
}

‎Chapter6/src/Command/Command.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Command;
2+
3+
// 커맨드 인터페이스
4+
public interface Command {
5+
Command NoCommand = (Command) () -> {};
6+
void execute();
7+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Command;
2+
3+
import Receiver.Light;
4+
5+
//커맨드 클래스
6+
public class LightOffCommand implements Command {
7+
Light light;
8+
9+
public LightOffCommand(Light light) {
10+
this.light = light;
11+
}
12+
13+
@Override
14+
public void execute() {
15+
light.off();
16+
}
17+
18+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Command;
2+
3+
import Receiver.Light;
4+
5+
//커맨드 클래스
6+
public class LightOnCommand implements Command {
7+
Light light;
8+
9+
public LightOnCommand(Light light) {
10+
this.light = light;
11+
}
12+
13+
@Override
14+
public void execute() {
15+
light.on();
16+
}
17+
18+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Command;
2+
3+
import Receiver.TV;
4+
5+
//커맨드 클래스
6+
public class TVOffCommand implements Command {
7+
TV tv;
8+
9+
public TVOffCommand(TV tv) {
10+
this.tv = tv;
11+
}
12+
13+
@Override
14+
public void execute() {
15+
tv.sleep();
16+
}
17+
18+
}

‎Chapter6/src/Command/TVOnCommand.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Command;
2+
3+
import Receiver.TV;
4+
5+
//커맨드 클래스
6+
public class TVOnCommand implements Command {
7+
TV tv;
8+
9+
public TVOnCommand(TV tv) {
10+
this.tv = tv;
11+
}
12+
13+
@Override
14+
public void execute() {
15+
tv.wakeUp();
16+
}
17+
18+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Invoker;
2+
3+
import Command.*;
4+
5+
class CommandSet{
6+
Command onCommand;
7+
Command offCommand;
8+
public CommandSet() {
9+
this.onCommand = Command.NoCommand;
10+
this.offCommand = Command.NoCommand;
11+
}
12+
13+
}
14+
15+
// invoker
16+
public class RemoteControl {
17+
CommandSet[] commandSets;
18+
19+
public RemoteControl() {
20+
commandSets = new CommandSet[7];
21+
for (int i=0; i<commandSets.length; i++){
22+
commandSets[i] = new CommandSet();
23+
}
24+
}
25+
26+
public void setCommand(int slot, Command onCommand, Command offCommand) {
27+
commandSets[slot].onCommand = onCommand;
28+
commandSets[slot].offCommand = offCommand;
29+
}
30+
31+
public void onButton(int slot) {
32+
commandSets[slot].onCommand.execute();
33+
}
34+
35+
public void offButton(int slot) {
36+
commandSets[slot].offCommand.execute();
37+
}
38+
}

‎Chapter6/src/Receiver/Light.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Receiver;
2+
3+
// Receiver
4+
public class Light {
5+
public void on() {
6+
System.out.println("Light - On");
7+
}
8+
9+
public void off() {
10+
System.out.println("Light - Off");
11+
}
12+
}

‎Chapter6/src/Receiver/TV.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Receiver;
2+
3+
// Receiver
4+
public class TV {
5+
public void wakeUp() {
6+
System.out.println("TV - Wake Up");
7+
}
8+
9+
public void sleep() {
10+
System.out.println("TV - Sleep");
11+
}
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.