π [λμμΈν¨ν΄] π» Strategy Pattern (μ λ΅μ ν¨ν΄)
π» Strategy Pattern (μ λ΅μ ν¨ν΄)
μλ
νμΈμ. π
μ€λμ Strategy Pattern ( μ λ΅μ ν¨ν΄ ) μ λν΄
μμλ³΄λ €κ³ ν©λλ€.
What is Strategy Pattern? (μ λ΅μ ν¨ν΄μ΄λ?)
- Strategy Pattern μ΄λ νμλ₯Ό ν΄λμ€λ‘ μΊ‘μνν΄ λμ μΌλ‘! νμλ₯Ό μμ λ‘κ² λ°κΏ μ μκ² ν΄μ£Όλ λμμΈ ν¨ν΄μ λλ€. μν©μ λ§κ² λ μ λ΅(μ μ± )μ λ§κ² λ νμλ₯Ό μ§μ ν΄μ€λλ€.
μμ
μ¬λ¬ μ ν¬κΈ°κ° μλ€κ³ κ°μ μ νμ λ μ ν¬κΈ°λ 곡ν΅μ μΌλ‘ 곡격μ ν λ¬΄κΈ°κ° μμ΅λλ€.
νμ§λ§ μ ν¬κΈ°μ μ’ λ₯μ λ°λΌ κ°κ° 무기λ λ€λ¦ λλ€.
λν μν©μ λ°λΌ λ¬΄κΈ°κ° λ¬λΌμ§ μλ μμ΅λλ€.
μ΄λ΄λ μ ν¬κΈ°μ 곡ν΅μ μΈ νμμ λν΄ κ°κ° μν©μ λ§κ² μ λ΅μ λ§κ² μ ν΄μ£Όλ κ²μ΄ μ λ΅μ ν¨ν΄μ λλ€.
μ λ΅μ ν¨ν΄μ μ¬μ© μ΄μ
μ ν¬κΈ° μΆμ ν΄λμ€
package strategy;
public abstract class Fighter implements AttackStrategy{
private String name;
public Fighter(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
F2 μ ν¬κΈ°
package strategy;
public class F2 extends Fighter{
public F2(String name) {
super(name);
}
@Override
public void attack() {
System.out.println("λ―Έμ¬μΌλ‘ 곡격");
}
}
μ λ΅μ ν¨ν΄μ μ¬μ©μ΄μ λ λ€μκ³Ό κ°μ΅λλ€. μμ²λΌ μμμ μ΄μ©νλ€λ©΄ κΈ°λ₯μ μΆκ°νκ±°λ λ³κ²½νλ €λ©΄ κΈ°μ‘΄μ μ½λλ₯Ό λ³κ²½ν΄μΌ ν©λλ€. μ΄λ OCP(Open-Closed Principle) λ²μΉμ μλ°°λ©λλ€.
λν μ΄λ° λ°©μμ μμ€ν μ΄ νμ₯μ΄ λμμ λ μ μ§λ³΄μλ₯Ό νλ€κ² ν©λλ€. μλ₯Όλ€μ΄ F2 μμΈμ λ€λ₯Έ μ¬λ¬ μ’ λ₯μ μ ν¬κΈ°λ€μ΄ μ겨λ λ μλ‘μ΄ μ’ λ₯μ λͺ¨λ μ ν¬κΈ°λ€μ΄ λν΄μ attck λ©μλλ₯Ό μΌμΌμ΄ μμ ν΄μΌνλ©° κ°μ λ©μλλ₯Ό μ¬λ¬ ν΄λμ€μμ λκ°μ΄ μ μνκ³ μμΌλ―λ‘ λ©μλμ μ€λ³΅λ¬Έμ κ° λ°μν©λλ€.
μ λ΅μ ν¨ν΄ μ μ©
μ ν¬κΈ° μΆμν΄λμ€
package strategy;
public abstract class Fighter {
private String name;
private AttackStrategy attackStrategy;
public Fighter(String name) {
this.name = name;
}
public void attack(){
attackStrategy.attack();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AttackStrategy getAttackStrategy() {
return attackStrategy;
}
public void setAttackStrategy(AttackStrategy attackStrategy) {
this.attackStrategy = attackStrategy;
}
}
MissileAttack ꡬν체
package strategy;
public class MissileAttack implements AttackStrategy{
@Override
public void attack() {
System.out.println("λ―Έμ¬μΌ 곡격");
}
}
GunAttack ꡬν체
package strategy;
public class GunAttack implements AttackStrategy{
@Override
public void attack() {
System.out.println("μ΄μΌλ‘ 곡격");
}
}
μ ν¬κΈ°μ 곡격 ννλ₯Ό μΊ‘μν νμ¬ μμ보λ€λ κ΅¬μ± (Composition)μ μ΄μ©ν©λλ€. 곡격μ΄λΌλ νμ(νλ)μ μΊ‘μν νμ¬ Flight ν΄λμ€μ λ³κ²½μ λ§μ΅λλ€. μλ‘μ΄ κΈ°λ₯μ μΆκ° μμ κΈ°μ‘΄μ μ½λμ μν₯μ λ―ΈμΉμ§ μκ³ AttackStrategy μΈν°νμ΄μ€λ₯Ό μμ λ°μ μλ‘μ΄ κΈ°λ₯μ ν΄λμ€λ₯Ό λ§λ€λ©΄ λ©λλ€. λ‘μ§μ λ λ¦μ μΌλ‘ κ΄λ¦¬νκΈ° μ½μ΅λλ€.
λν setterλ₯Ό μ΄μ©ν΄ μΈλΆμμ μν©μ λ§κ² νμμ λ°λΌ μ λ΅μ μΌλ‘ λμν κΈ°λ₯μ λ³κ²½ν μ μμ΅λλ€. μ΄λ‘μΈν΄ μμ€ν μ΄ μ μ°νκ² λ³κ²½λκ³ νμ₯λ μ μμ΅λλ€.
package strategy;
public class main {
public static void main(String[] args) {
Fighter f1 = new F1("F1");
Fighter f2 = new F2("F2");
f1.setAttackStrategy(new MissileAttack());
f2.setAttackStrategy(new GunAttack());
System.out.println(f1.getName()+" μ ν¬κΈ°");
f1.attack();
System.out.println(f2.getName()+" μ ν¬κΈ°");
f2.attack();
f2.setAttackStrategy(new MissileAttack());
System.out.println(f2.getName()+" μ ν¬κΈ°");
f2.attack();
}
}
λ
μ€λμ μ λ΅μ ν¨ν΄ (strategy pattern)μ λν΄ μμ보μμ΅λλ€.
κ°μ¬ν©λλ€. π
Reference
https://victorydntmd.tistory.com/292
https://flowarc.tistory.com/entry/1-Strategy-Pattern