//Strategy Pattern 스트래티지 패턴 예제
interface QuackBehavior
{
public void quack();
} // 공통 기능을 정의하는 인터페이스 (오리는 공통적으로 꽥꽥 거리는 행동을 한다)
/////////////////// Quack class ///////////////////
class Quack implements QuackBehavior
{
public void quack(){
System.out.println("꽥!");
}
} // 인터페이스를 상속 받아서 꽥꽥 이라는 기능을 정의한다. (오버라이딩)
///////////////// MuteQuack class ////////////////////
class MuteQuack implements QuackBehavior
{
public void quack(){
System.out.println("조용!");
}
}
//
//////////////////////////////////////////////////////////////
class RubberQuack implements QuackBehavior
{
public void quack(){
System.out.println("러버 꽥!!");
}
}
//////////////////////////////////////////////////////////////
class RubberFlyBehavior implements FlyBehavior{
public void fly(){
System.out.println("러버~!");
}
}
//////////////////////////////////////////////////////////////
interface FlyBehavior
{
public void fly();
}
//////////////////////////////////////////////////////////////
class FlyWithMings implements FlyBehavior{
public void fly(){
System.out.println("날아간다~~!");
}
}
//////////////////////////////////////////////////////////////
class FlyNoWay implements FlyBehavior{
public void fly(){
System.out.println("꼼짝~!");
}
}
//////////////////////////////////////////////////////////////
class Duck{
FlyBehavior fb;
QuackBehavior qb;
public void performQuack(){
qb.quack();
}
public void performFly(){
fb.fly();
}
public void shotByGun(){
qb = new MuteQuack();
fb = new FlyNoWay();
}
void setFlyBehavior(FlyBehavior fb){
this. fb = fb;
}
}
//////////////////////////////////////////////////////////////
class MallardDuck extends Duck
{
MallardDuck(){
qb = new Quack();
fb = new FlyWithMings();
};
}
//////////////////////////////////////////////////////////////
class DecoyDuck extends Duck{
DecoyDuck(){
qb = new MuteQuack();
fb = new FlyNoWay();
}
}
//////////////////////////////////////////////////////////////
class RubberDuck extends Duck{
RubberDuck(){
qb = new RubberQuack();
fb = new RubberFlyBehavior();
}
}
//////////////////////////////////////////////////////////////
class FlyRocketPowered implements FlyBehavior
{
public void fly(){
System.out.println("날아간다 슈웅~~!!!");
}
}
class MiniDuckSimulator
{
public static void main(String[] args)
{
Duck d[] = new Duck[10];
for(int i=0; i<10; i++){
int x = (int)(Math.random()*10);
if(x%3==0){
d[i]=new MallardDuck();
}else if(x%3==1){
d[i]=new DecoyDuck();
}else{
d[i]=new RubberDuck();
}
/*
Duck m = new MallardDuck();
Duck d = new DecoyDuck();
Duck r = new RubberDuck();
m.performQuack();
m.performFly();
d.performQuack();
d.performFly();
r.performQuack();
r.performFly();
*/
}
d[3].shotByGun();
d[5].shotByGun();
d[9].shotByGun();
for(int j=0; j<10; j++){
System.out.println((j+1)+" 번째 오리가 꽥꽥거린다");
d[j].performQuack();}
System.out.println("\n");
for(int j=0; j<10; j++){
System.out.println((j+1)+" 번째 오리가 날아다닌다");
d[j].performFly();}
Duck r = new RubberDuck();
r.setFlyBehavior(new FlyRocketPowered());
System.out.println("\n");
r.performQuack();
r.performFly();
}
}