Download - Command pattern 김우진
![Page 1: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/1.jpg)
Command Pattern
NHN NEXT 김우진
요구사항을 객체로 캡슐화 하는 패턴.
![Page 2: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/2.jpg)
!
시작
![Page 3: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/3.jpg)
다음과 같은 리모콘의 S/W제작을 의뢰 받았습니다.
에어컨TV
오디오현관문거실 등
![Page 4: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/4.jpg)
기능은 간단합니다.
에어컨TV
오디오현관문거실 등
1. 휠을 통한 가전제품 선택 2. 버튼을 통한 On/Off
![Page 5: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/5.jpg)
먼저, 가전제품과 직접적으로 연결하기로 합니다.
에어컨TV
오디오현관문거실 등
public void airconButtonClick(Aircon aircon) { if(aircon.isOn()) { aircon.off(); } else { aircon.on(); }}
![Page 6: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/6.jpg)
먼저, 가전제품과 직접적으로 연결하기로 합니다.
에어컨TV
오디오현관문거실 등
public void audioButtonClick(Audio audio) { if(audio.isPlay()) { audio.stop(); } else { audio.play(); }}
![Page 7: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/7.jpg)
하다보니까…
TV오디오현관문거실등???
public void ???ButtonClick(??? ???) { if(???()) { ???(); } else { ???(); }}
가전제품마다 메소드가 달라, 새로운 기능이 추가되면,
매번 추가해 주어야 합니다.
![Page 8: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/8.jpg)
!
그래서…
하나의 메소드로, 여러 가전제품을 조작 할 수 없을까?
remote.buttonClick();remote.tvButtonClick();remote.audioButtonClick();remote.???ButtonClick();
![Page 9: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/9.jpg)
!
그래서 Command Pattern!
요구사항을 객체로 캡슐화합니다.
public interface Command { public void execute();}
![Page 10: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/10.jpg)
먼저, 가전제품 별로 Command객체를 구현합니다.
에어컨TV
오디오현관문거실 등
public class AirconCommand implements Command { Aircon aircon; public airconCommand (Aircon aircon) { this.aircon = aircon; }! @Override public void execute() { if(aircon.isOn()) { aircon.off(); } else { aircon.on(); } }}
![Page 11: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/11.jpg)
다시 기능을 볼까요?
에어컨TV
오디오현관문거실 등
1. 휠을 통한 가전제품 선택 !
!
2. 버튼을 통한 On/Off
Command객체를 Remote객체에 넘깁니다.
Remote객체에서 buttonClicked메소드를 실행합니다.remote.buttonClick();
remote.setCommand(command);
![Page 12: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/12.jpg)
Command객체를 Remote객체에 넘깁니다.(선택)
// In main method// 객체 생성Remote remote = new Remote();Aircon aircon = new Aircon();AirconCommand ac = new AirconCommand(aircon);!// Command 객체 넘기기 remote.setCommand(ac);
![Page 13: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/13.jpg)
buttonClicked메소드를 실행합니다.(on/off)
// 버튼 누르기remote.buttonClick();
![Page 14: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/14.jpg)
다른 가전제품을 조작하려면?
// 객체 생성tvCommand tc = new AirconCommand(tv);!// Command 객체 넘기기 remote.setCommand(tc);!// 버튼 누르기remote.buttonClick();
![Page 15: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/15.jpg)
참고 : Remote Class
public class remoteTest { Command slot; public void setCommand(Command command) { slot = command; } public void buttonClick() { slot.execute(); }}
![Page 16: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/16.jpg)
이제 새로운 제품이 추가되면, Command Interface를 구현하기만 하면 됩니다.
참 쉽죠?
![Page 17: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/17.jpg)
Class Diagram
리모콘
사용자
가전제품
![Page 18: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/18.jpg)
!
Memento Pattern 복습
![Page 19: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/19.jpg)
Memento Pattern 이것만 알면 된다!
1. 상태를 객체에 담아 저장한다.
![Page 20: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/20.jpg)
Memento Pattern 이것만 알면 된다!
2. 상태객체를 통해 이전 상태로 돌아 갈 수 있다.
![Page 21: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/21.jpg)
Memento Pattern 이것만 알면 된다!
3. 상태객체를 생성하는 곳과 저장하고 관리하는 곳이 다르다.
![Page 22: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/22.jpg)
Memento Pattern Class Diagram
상태객체생성, 변경 저장, 관리
![Page 23: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/23.jpg)
!
Command Pattern + Memento Pattern
![Page 24: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/24.jpg)
Command Pattern + Memento Pattern
public interface Command { public void execute(); public void undo();}
먼저 execute()에 반대되는 undo()를 구현한다. ex) excute() - on() : undo() - off()
![Page 25: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/25.jpg)
Command Pattern + Memento Pattern
Main Method에서 List에 Command객체를 저장한다.
List<Command> commandList = new ArrayList<Command>();commandList.add(ac);
![Page 26: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/26.jpg)
Command Pattern + Memento Pattern
List에서 pop()하여 undo()를 실행하면, 이전상태로 돌아간다.
commandList.pop().undo();
![Page 27: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/27.jpg)
Command Pattern + Memento Pattern
여기서 Command Object를 상태를 저장하는 하나의 Memento라고 볼 수 있다.
![Page 28: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/28.jpg)
!
마지막으로
![Page 29: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/29.jpg)
Command Pattern 이것만 기억하자!
Command Pattern은 요구사항을 객체화 하여,
Client의 코드와 Receiver의 코드를 분리하는 Pattern 이다.
Client (사용자)
Receiver (가전제품)
Invoker (리모콘)
Command 객체 사용
![Page 30: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/30.jpg)
!
끝, Q & A
![Page 31: Command pattern 김우진](https://reader035.vdocuments.site/reader035/viewer/2022062514/55a060331a28ab462e8b4730/html5/thumbnails/31.jpg)
참고
Head First Design Patterns - O’REILLY
WIKIPEDIA