본문으로 바로가기

6. Command Pattern

category Design Pattern/Head FIrst Design Pattern 2019. 5. 20. 21:20

6. Command Pattern (커맨드 패턴)


6.1 Command Pattern의 다이어그램



- 실제 기능을할 메소드를 가진 객체는 receiver 클래스입니다.

- command interface를 구현한 각 concreteCommand(구상커맨드클래스)들은 receiver들의 reference를 가졌고 receiver들의 메소드를 호출합니다.

- invoker class는 setCommand() 메소드를 이용해 concreteCommand들을 설정하고 execute() 메소드를 호출함으로서 receiver의 메소드를 호출합니다.


6.2 Simple RemoteControl

invoker, command, receiver가 어떻게 연결되는지 간단한 리모컨 예를통해 보여드릴게요.

- main class 1

- command interface 1

- concrete command class 1

- receiver 1

- invoker 1

총 5개의 클래스


6.2.1 reciever class

실제 원하는 기능 메소드를 가진 클래스입니다.


6.2.2 command interface

구상 커맨드 클래스들이 구현해야하는 커맨드 인터페이스 입니다.


6.2.3 concrete command class

- 커맨드 인터페이스를 구현한 구상커맨드 클래스입니다.

- 각 구상커맨드 클래스의 execute()메소드와 receiver class의 기능 메소드가 연결됩니다.

- 구상커맨드 클래스는 연결하고자 하는 receiver의 reference를 가지고 있습니다.


6.2.4 invoker class

구상커맨드 클래스들을 설정하여 사용하는 클래스입니다.


6.2.5 main class

test class입니다.


6.3 Remotecontrol

위의 simple remotecontol의 경우 하나의 command만 set가능합니다. 

하지만 invoker에 여러개의 command를 set하여 사용할 수 있도록 확장해보겠습니다.


6.3.1 receiver class


6.3.2 command interface


6.3.3 concrete command calss


6.3.4 invoker class


6.3.5 main class


'Design Pattern > Head FIrst Design Pattern' 카테고리의 다른 글

8. Facade Pattern  (0) 2019.05.25
7. Adaptor Pattern  (0) 2019.05.23
4. Factory Pattern - (1) SImple Factory  (0) 2019.04.21
3. Decorator Pattern  (0) 2019.04.21
2. Observer Pattern  (0) 2019.04.21