Strategy Pattern π
κ°μ κΈ°λ₯μ΄μ§λ§ μλ‘ λ€λ₯Έ μ λ΅μ κ°μ§ classλ€μ κ°κ° μΊ‘μννμ¬ μνΈκ΅νν μ μλλ‘ νλ ν¨ν΄
ꡬ쑰
Context
Strategy
ConcreteStrategy
μ₯μ
μν©μ λ°λΌ μ μ§ λ³΄μ μ©μ΄
μκ³ λ¦¬μ¦ κ΅¬νλΆμ μ¬μ©λΆκ° λΆλ¦¬
λ¨μ
νμ© μν©
μκ³ λ¦¬μ¦(μ λ΅)μ ν΄λΌμ΄μΈνΈκ° μ ν΄μΌ ν λ
λμΌ κ³μ΄μ μκ³ λ¦¬μ¦λ€μ μΈν°νμ΄μ€λ‘ ν΅μΌνμ¬ μ 곡ν λ
ꡬν
interface CoffeeStrategy {
public abstract String brew();
}
public class AmericanoStrategy implements CoffeeStrategy {
private static final String AMERICANO = "μλ©λ¦¬μΉ΄λ
Έ";
@Override
public String brew() {
return AMERICANO;
}
}
public class CafeLatteStrategy implements CoffeeStrategy {
private static final String CAFE_LATTE = "μΉ΄νλΌλΌ";
@Override
public String brew() {
return CAFE_LATTE;
}
}
public class CoffeeMachine {
public String brew(CoffeeStrategy coffeestrategy) {
return coffeestrategy.brew();
}
}
public class Main {
public static void main(String[] args) {
CoffeeMachine coffeeMachine = new CoffeeMachine();
String americano = coffeeMachine.brew(americanoButton());
System.out.println(americano);
String cafelatte = coffeeMachine.brew(cafeLatteButton());
System.out.println(cafelatte);
}
public static CoffeeStrategy americanoButton() {
return new AmericanoStrategy();
}
public static CoffeeStrategy cafeLatteButton() {
return new CafeLatteStrategy();
}
}