반응형

전략 패턴

-> 

 

1. 동일 계열의 알고리즘을 정의하고

동적으로 행위의 수정이 필요한 경우

전략을 바꾸는것으로 행위의 수정이 가능하게 만드는 패턴 

 

https://ko.wikipedia.org/wiki/%EC%A0%84%EB%9E%B5_%ED%8C%A8%ED%84%B4

 

example

public class StrategyPatternWiki
{
    public static void Main(String[] args)
    {
        Customer firstCustomer = new Customer(new NormalStrategy());

        // Normal billing
        firstCustomer.Add(1.0, 1);

        // Start Happy Hour
        firstCustomer.Strategy = new HappyHourStrategy();
        firstCustomer.Add(1.0, 2);

        // New Customer
        Customer secondCustomer = new Customer(new HappyHourStrategy());
        secondCustomer.Add(0.8, 1);
        // The Customer pays
        firstCustomer.PrintBill();

        // End Happy Hour
        secondCustomer.Strategy = new NormalStrategy();
        secondCustomer.Add(1.3, 2);
        secondCustomer.Add(2.5, 1);
        secondCustomer.PrintBill();
    }
}

명령 패턴

-> 

 

요청 자체를 캡슐화

실행될 기능을 캡슐화해 기능 실행을 요구하는 호출자와 실제 기능을 실행하는 수신자 클래스 사이의 의존성 제거

 

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

 

/*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 light=new Light();
       Command switchUp=new TurnOnLightCommand(light);
       Command switchDown=new TurnOffLightCommand(light);

       Switch s=new Switch(switchUp,switchDown);

       s.flipUp();
       s.flipDown();
   }
}

 

둘의 차이점?

 

개인적으로 둘의 쓰임새는 비슷하다고 느껴서 엄청난 혼동이 왔습니다 

 

스텍오버플로우 형님들에 따르면 

 

Command - Open or Close [action change]

Typically the Command pattern is used to make an object out of what needs to be done

Strategy - Quicksort or Mergesort [algo change]

The Strategy pattern, on the other hand, is used to specify how something should be done

 

라는데 요약해보자면

 

커맨드 패턴 -> 다양한 action이 있고 어떤 action'(what)을 사용하냐에 중점

 

전략 패턴 -> 다양한 algorithm이 있고 '어떻게'(how) 해결하냐에 중점

 

인거 같아요.?

 

 

reference

 

 

https://gmlwjd9405.github.io/2018/07/07/command-pattern.html

 

[Design Pattern] 커맨드 패턴이란 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

https://stackoverflow.com/questions/4834979/difference-between-strategy-pattern-and-command-pattern

 

Difference between Strategy pattern and Command pattern

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.

stackoverflow.com

 

반응형

+ Recent posts