ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Design Pattern] Strategy Pattern 전략 패턴
    Study/Design Pattern 2021. 7. 18. 18:36
     

     

    략 패턴이란 같은 기능을 하는 알고리즘들을 캡슐화하여 서로 간 상호 교체가 가능하도록 하게 하는 디자인 패턴입니다. 이를 통해 새로운 알고리즘이 추가되어도 기존의 코드를 수정할 필요 없이 쉽게 추가가 가능해집니다. 이는 SOLID 원칙에서 수정, 삭제에는 닫혀있고 추가에는 개방되어야 하는 개방-폐쇄 원칙(OCP)을 만족합니다.

     

     

     

    //Champion 클래스
     public abstract class Champion
     {
       private String name;
       public Champion(String name)
       {
         this.Champion = Champion;
       }
       public String getName()
       {
         return name;
       }
       public abstract void attack();
     }
    //티모 클래스
     public class 티모 extends Champion
     {
       public 티모(String name) 
       {
         super(name);
       }
       public void attack()
       {
         System.out.println("독침 공격!");
       }
     }
    //피즈 클래스
     public class 피즈 extends Champion
     {
       public 피즈(String name) 
       {
         super(name);
       }
       public void attack()
       {
         System.out.println("창 공격!");
       }
     }

    위와 같이 챔피언 추상 클래스가 있고, 이를 확장하는 티모, 피즈 구현 클래스가 있다고 가정해보겠습니다. attack()이라는 공통된 메소드가 있는데 만약 티모의 공격 방법이 바뀌면 어떻게 해야 할까요? 만약 티모 클래스에서 코드를 직접 수정하면 OCP 원칙에 위배되게 됩니다. 그래서 아래와 같이 전략 패턴을 이용해보겠습니다.

     

     

     

    //Champion 클래스
     public abstract class Champion
     {
       private String name;
       private AttackStrategy attackstrategy;
       
       public Champion(String name)
       {
         this.Champion = Champion;
       }
       public String getName()
       {
         return name;
       }
       public void setAttackStrategy(AttackStrategy attackstrategy)
       {
         this.AttackStrategy = attackstrategy;
       }
       public abstract void attack(){
       	 attackstrategy.attack();
       }
     }
    //티모 클래스
     public class 티모 extends Champion
     {
       public 티모(String name) 
       {
         super(name);
       }
     }
    //피즈 클래스
     public class 피즈 extends Champion
     {
       public 피즈(String name) 
       {
         super(name);
       }
     }
    //AttackStrategy 인터페이스
     interface AttackStrategy
     {
       public void attack();
     }
    //Posion 클래스
     public class Posion implements AttackStrategy
     {
       public void attack()
       {
         System.out.println("독침 공격");
       }
     }
    //Spear 클래스
     public class Spear implements AttackStrategy
     {
       public void attack()
       {
         System.out.println("창 공격");
       }
     }
    //Rifle 클래스
     public class Rifle implements AttackStrategy
     {
       public void attack()
       {
         System.out.println("라이플 공격");
       }
     }

     

    위와 같이 챔피언의 attack 메소드를 하위 클래스에서 직접 구현하는것이 아닌 AttackStrategy라는 인터페이스로 추상화 하여 따로 구현하게 됩니다. 그래서 챔피언 객체의 attack 메소드를 사용하기전 AttackStrategy 객체를 주입해주어 attack 전략을 아래와 같이 클라이언트에서 고를수 있게해줍니다.

     

     public class Client
     {
       public static void main(String[] args)
       {
         Champion teemo = 티모("티모");
         Champion fizz = 피즈("피즈");
         
         teemo.setAttackStrategy(new PosionStrategy());
         fizz.setAttackStrategy(new SpearStrategy());
         
         System.out.println(teemo.getName());
         teemo.attack();
         
         System.out.println(fizz.getName());
         fizz.attack();
         
         teemo.setAttackStrategy(new RifleStrategy());
         System.out.println(teemo.getName());
         teemo.attack();
       }
     }
     
     // Output
     티모
     독침 공격
     
     피즈
     창 공격
     
     티모
     라이플 공격
     
     
     

    댓글

Designed by Tistory.