레이블이 커맨드 패턴인 게시물을 표시합니다. 모든 게시물 표시
레이블이 커맨드 패턴인 게시물을 표시합니다. 모든 게시물 표시

2016년 2월 12일 금요일

커맨드 패턴 (command pattern)


커맨드 패턴(Command pattern)이란 요청을 객체의 형태로 캡슐화하여 사용자가 보낸 요청을 나중에 이용할 수 있도록 매서드 이름, 매개변수 등 요청에 필요한 정보를 저장 또는 로깅, 취소할 수 있게 하는 패턴이다. 

어떠한 행동에 대한 정의 들을 제어 할때 사용하면 좋다 .

- 명령(command), 수신자(receiver), 발동자(invoker), 클라이언트(client)의 네개의 용어가 항상 따른다.
-  요청과 수행을 분리시켜 느슨한 결합을 유지한다.

- 일련의 행동을 특정 Receiver하고 연결을 통해 캡슐화하고 execute()라는 메소드 하나만 외부에 공개한다.


장점
- 요청부분과 요청실행부분을 분리(낮은 결합도, 독립적 변경 가능)
- 실행취소, 보관, 로그 생성 가능

* 전략패턴(strategy pattern)과 비슷하지만 커맨드 패턴은 전달받은 객체가 하는 일에 대해 정확히 알지 못한다.

 - wiki 참조 -

// 자바스크립트로 구현 
<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
  <script>
 // 커맨드 패턴
 // 리모컨 만들기
 // 1. 리시버에서 해당 기능 구현 
 // 2. command 에 interface 생성 
 // 3. command에 recevier에서 받은 객체를 실행하는 동작에 대해 execute에 정의
 // 4. invoker 에서 setcommand 명령에 대해 저장하는 처리
 // 5. invoker에 목적에 맞는 custom메서드를 생성 (ex> 리모컨켜기, 리모컨 끄기 등)

  function r(msg){
   return function(){
    document.body.innerHTML += (msg+"<br />");
   }
  }

  var client, recevier, invoker, command;
 
  function Light(){}  //recevier
  Light.prototype = {
   "on" : r("on"),
   "off" : r("off")
  };
  //(new Light).on();

  function lightOnCommand(receiver_obj){  //command
   this.execute = function(){
    receiver_obj.on();
   };
   this.undo = function(){
    receiver_obj.off();
   };
  }

  function lightOffCommand(receiver_obj){  //command
   this.execute = function(){
    receiver_obj.off();
   };
   this.undo = function(){
    receiver_obj.on();
   };
  }

  function RemoteControl(){    //invoker
   this.setCommand = function(cmd){
    this.obj = cmd;
   }
   this.press = function(){
    this.obj.execute();
    this.undocommand = this.obj;
   }
   this.undo = function(){
    this.undocommand.undo();
   }
  }


  client = function(){
   var light =  new Light;
   var lightOn = new lightOnCommand(light);
   var lightOff = new lightOffCommand(light);
   var remoteControl = new RemoteControl;
    remoteControl.setCommand(lightOn);
    remoteControl.press();
    remoteControl.undo();

    remoteControl.setCommand(lightOff);
    remoteControl.press();
    remoteControl.undo();

  }();

  </script>
 </body>
</html>