로봇 만들기
AttackStrategy.class
public interface AttackStrategy { void attack(); }
MissileStrategy.class
public class MissileStrategy implements AttackStrategy {
public void attack() { System.out.println("I have Missile."); }
}
PunchStrategy.class
public class PunchStrategy implements AttackStrategy {
public void attack() { System.out.println("I have strong punch."); }
}
MovingStrategy.class
public interface MovingStrategy { void move(); }
FlyingStrategy.class
public class FlyingStrategy implements MovingStrategy {
public void move() { System.out.println("I can fly."); }
}
WalkingStrategy.class
public class WalkingStrategy implements MovingStrategy {
public void move() { System.out.println("I can only walk."); }
}
Robot.class
public abstract class Robot {
private String name;
private AttackStrategy attackStrategy;
private MovingStrategy movingStrategy;
public Robot(String name) { this.name = name; }
public String getName() { return name; }
public void attack() { attackStrategy.attack(); }
public void move() { movingStrategy.move(); }
// 집약 관계, 전체 객체가 메모리에서 사라진다 해도 부분 객체는 사라지지 않는다.
// setter 메서드
public void setAttackStrategy(AttackStrategy attackStrategy) {
this.attackStrategy = attackStrategy; }
public void setMovingStrategy(MovingStrategy movingStrategy) {
this.movingStrategy = movingStrategy; }
}
TaekwonV.class
public class TaekwonV extends Robot {
public TaekwonV(String name) { super(name); }
}
Atom.class
public class Atom extends Robot {
public Atom(String name) { super(name); }
}
Main.class
public static void main(String[] args) {
Robot taekwonV = new TaekwonV("TaekwonV");
Robot atom = new Atom("Atom");
/* 수정된 부분: 전략 변경 방법 */
taekwonV.setMovingStrategy(new WalkingStrategy());
taekwonV.setAttackStrategy(new MissileStrategy());
atom.setMovingStrategy(new FlyingStrategy());
atom.setAttackStrategy(new PunchStrategy());
/* 아래부터는 동일 */
System.out.println("My name is " + taekwonV.getName());
taekwonV.move();
taekwonV.attack();
System.out.println();
System.out.println("My name is " + atom.getName());
atom.move();
atom.attack();
}
실행결과
>>>My name is TaekwonV
>>>I can only walk.
>>>I have Missile.
>>>My name is Atom
>>>I can fly.
>>>I have strong punch.
참조: