자바생
Published 2022. 3. 4. 21:11
Strategy Pattern Design Pattern
728x90

우리는 게임할 때, strategy(전략)을 자주 바꿔서 게임을 하게된다.

strategy pattern은 전략을 쉽게 바꿀 수 있도록 해주는 디자인 패턴이다.

 

변화되는 부분을 각각 클래스 set으로 만든다.

해당 책에서와 같이 공격하는 클래스, 움직이는 클래스를 생성

 

공격을 하는(발을 찬다. 주먹을 찌른다) 행동을 각 클래스로 생성한 뒤, 공격하는 클래스에 상속 관계를 맺는다.

 

다양한 방식으로 특정 작업을 수행하는 클래스를 선택하여 strategy라고 하는 별도의 클래스로 추출

 

context = origin class

context에는 strategy를 참조하는 필드가 존재해야함

  • context가 strategy를 직접적으로 실행하는 것이 아님
    • context는 strategy에 대해 잘 모름
  • context에서 적합한 strategy를 선택할 수 없음
  • client가 원하는 strategy를 context에 전달
package StrategyPattern;

public interface DiscountPolicy {

    public int discount(int price);
}
package StrategyPattern;

public class FixDiscountPolicy implements DiscountPolicy{
    @Override
    public int discount(int price) {
        return price - 1000;
    }
}
package StrategyPattern;

public class RateDiscountPolicy implements DiscountPolicy{
    @Override
    public int discount(int price) {
        return (int)(price * 0.8);
    }
}
package StrategyPattern;

public enum MemberShip {
    VIP, GENERAL
}
package StrategyPattern;

public enum MemberShip {
    VIP, GENERAL
}
package StrategyPattern;

public class Member {

    private int price;
    private DiscountPolicy discountPolicy;
    private MemberShip memberShip;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MemberShip getMemberShip() {
        return memberShip;
    }

    public void setMemberShip(MemberShip memberShip) {
        this.memberShip = memberShip;
    }

    public int getPrice() {
        if (memberShip == MemberShip.GENERAL) {
            discountPolicy = new FixDiscountPolicy();
        } else {
            discountPolicy = new RateDiscountPolicy();
        }

        return discountPolicy.discount(price);
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public DiscountPolicy getDiscountPolicy() {
        return discountPolicy;
    }

    public void setDiscountPolicy(DiscountPolicy discountPolicy) {
        this.discountPolicy = discountPolicy;
    }
}
package StrategyPattern;

import org.junit.Test;

import static StrategyPattern.MemberShip.*;
import static org.junit.Assert.*;

public class MemberTest {

    @Test
    public void 일반회원() throws Exception {
        //given
        Member m1 = new Member();
        m1.setPrice(10000);
        m1.setName("woo");

        //when
        m1.setMemberShip(GENERAL);
        int price = m1.getPrice();

        //then
        assertEquals(price, 9000);
    }

    @Test
    public void VIP회원() throws Exception {
        //given
        Member m1 = new Member();
        m1.setPrice(10000);
        m1.setName("woo");

        //when
        m1.setMemberShip(VIP);
        int price = m1.getPrice();

        //then
        assertEquals(price, 8000);
    }
}

Spring과 Strategy Pattern

https://www.up-2date.com/post/strategy

728x90

'Design Pattern' 카테고리의 다른 글

Decorator Pattern  (0) 2022.11.06
Template Callback Pattern  (0) 2022.11.01
Singleton Pattern  (0) 2022.03.17
Command Pattern  (0) 2022.03.15
State Pattern  (0) 2022.03.12
profile

자바생

@자바생

틀린 부분이 있다면 댓글 부탁드립니다~😀

검색 태그