ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 행위패턴 : Command 패턴
    디자인패턴 2013. 2. 26. 09:30
    반응형

    분류 : 행위패턴(Behavioral Patterns)


    정의 :

    요청을 객체 형태로 캡슐화해서 서로 요청이 다른 사용자의 매개변수와, 요청 저장 또는 로깅, 명령의 취소를 지원하게 하는 패턴


    용도 :

    • GUI 버튼과 메뉴 아이템
    • 매크로 기록
    • 다단계 Undo
    • 네트워크를 통한 명령 전송
    • 병렬처리
    • 진행상태 바
    • 스레드 풀
    • 트랜잭션형 행위
    • 마법사 형태의 처리


    소스

    
    //커맨드 인터페이스
    public interface Command {
    	void execute();
    }
    
    
    // 라이트를 켜는 명령(커맨드) - ConcreteCommand #1 
    public class FlipUpCommand implements Command {
       private Light theLight;
     
       public FlipUpCommand(Light light) {
          this.theLight = light;
       }
     
       public void execute(){
          theLight.turnOn();
       }
    }
    
    // 라이트를 끄는 명령(커맨드) - ConcreteCommand #2 
    public class FlipDownCommand implements Command {
       private Light theLight;
     
       public FlipDownCommand(Light light) {
          this.theLight = light;
       }
     
       public void execute() {
          theLight.turnOff();
       }
    }
    
    
    // The Receiver class 
    public class Light {
       public Light() {
       }
     
       public void turnOn() {
          System.out.println("The light is on");
       }
     
       public void turnOff() {
          System.out.println("The light is off");
       }
    }
    
    
    import java.util.List;
    import java.util.ArrayList;
     
    // The Invoker class 
    public class Switch {
       private List history = new ArrayList();
     
       public Switch() {
       }
     
       public void storeAndExecute(Command cmd) {
          this.history.add(cmd); // optional 
          cmd.execute();        
       }
    }
    
    
    // 테스트 클래스  
    public class PressSwitch {
       public static void main(String[] args){
          Light lamp = new Light();
          Command switchUp = new FlipUpCommand(lamp);
          Command switchDown = new FlipDownCommand(lamp);
     
          Switch s = new Switch();
     
          //이 부분 주석 코드 변경하면서 작동확인.
          String CommandStr = "ON";
    //      String CommandStr = "OFF";
          
          try {
             if (CommandStr.equalsIgnoreCase("ON")) {
                s.storeAndExecute(switchUp);
             }
             else if (CommandStr.equalsIgnoreCase("OFF")) {
                s.storeAndExecute(switchDown);
             }
             else {
                System.out.println("Argument \"ON\" or \"OFF\" is required.");
             }
          } catch (Exception e) {
             System.out.println("Arguments required.");
          }
       }
    }
    



    참고자료

    http://ko.wikipedia.org/wiki/%EC%BB%A4%EB%A7%A8%EB%93%9C_%ED%8C%A8%ED%84%B4

    http://en.wikipedia.org/wiki/Command_pattern


    반응형

    '디자인패턴' 카테고리의 다른 글

    행위패턴 : Iterator 패턴  (0) 2013.02.27
    행위패턴 : Interpreter 패턴  (0) 2013.02.27
    행위패턴 : Chain of Responsibility 패턴  (0) 2013.02.07
    구조패턴 : Proxy 패턴  (0) 2013.02.06
    구조패턴 : Flyweight 패턴  (0) 2013.02.05

    댓글

Designed by Tistory.