커맨드 패턴

위키백과, 우리 모두의 백과사전.
이동: 둘러보기, 찾기

커맨드 패턴(Command pattern)이란 요청을 객체의 형태로 캡슐화하여 서로 요청이 다른 사용자의 매개변수와, 요청 저장 또는 로깅, 그리고 연산의 취소를 지원하게 만드는 패턴이다.

목차

[편집] 구조

Command Design Pattern Class Diagram.png

[편집]

[편집] C++

#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
class Command{
  public:
   virtual void execute(void) =0;
   virtual ~Command(void){};
};
 
class Ingredient : public Command {
  public:
   Ingredient(string amount, string ingredient){
      _ingredient = ingredient;
      _amount = amount;
   }
   void execute(void){
      cout << " *Add " << _amount << " of " << _ingredient << endl;
   }
  private:
   string _ingredient;
   string _amount;
};
 
class Step : public Command {
  public:
   Step(string action, string time){
      _action= action;
      _time= time;
   }
   void execute(void){
      cout << " *" << _action << " for " << _time << endl;
   }
  private:
   string _time;
   string _action;
};
 
class CmdStack{
  public:
   void add(Command *c) {
      commands.push_back(c);
   }
   void createRecipe(void){
      for(vector<Command*>::size_type x=0;x<commands.size();x++){
         commands[x]->execute();
      }
   }
   void undo(void){
      if(commands.size() > 1) {
         commands.pop_back();
      }
      else {
         cout << "Can't undo" << endl;
      }
   }
  private:
   vector<Command*> commands;
};
 
int main(void) {
   CmdStack list;
 
   //Create ingredients
   Ingredient first("2 tablespoons", "vegetable oil");
   Ingredient second("3 cups", "rice");
   Ingredient third("1 bottle","Ketchup");
   Ingredient fourth("4 ounces", "peas");
   Ingredient fifth("1 teaspoon", "soy sauce");
 
   //Create Step
   Step step("Stir-fry","3-4 minutes");
 
   //Create Recipe
   cout << "Recipe for simple Fried Rice" << endl;
   list.add(&first);
   list.add(&second);
   list.add(&step);
   list.add(&third); 
   list.undo();
   list.add(&fourth);
   list.add(&fifth);
   list.createRecipe();
   cout << "Enjoy!" << endl;
   return 0;
}

[편집] 자바

/*the Invoker class*/
public class Switch {
    private Command flipUpCommand;
    private Command flipDownCommand;
 
    public Switch(Command flipUpCmd,Command flipDownCmd){
            this.flipUpCommand=flipUpCmd;
            this.flipDownCommand=flipDownCmd;
           }
 
    public void flipUp(){
         flipUpCommand.execute();
    }
 
    public void flipDown(){
         flipDownCommand.execute();
    }
}
 
/*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");
     }
}
 
 
/*the Command interface*/
 
public interface Command{
    void execute();
}
 
 
/*the Command for turning on the light*/
 
public class TurnOnLightCommand implements Command{
   private Light theLight;
 
   public TurnOnLightCommand(Light light){
        this.theLight=light;
       }
 
   public void execute(){
      theLight.turnOn();
   }
}
 
/*the Command for turning off the light*/
 
public class TurnOffLightCommand implements Command{
   private Light theLight;
 
   public TurnOffLightCommand(Light light){
        this.theLight=light;
       }
 
   public void execute(){
      theLight.turnOff();
   }
}
 
/*The test class*/
public class TestCommand{
   public static void main(String[] args){
       Light l=new Light();
       Command switchUp=new TurnOnLightCommand(l);
       Command switchDown=new TurnOffLightCommand(l);
 
       Switch s=new Switch(switchUp,switchDown);
 
       s.flipUp();
       s.flipDown();
   }
}