Strategy Pattern

Definition-defines a family of algorithms, encapsulate each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Key points: separate different parts/behaviors and encapsulate each part/behavior

Advantage: not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime .

Example1 (quote from First Head In Design Pattern) :

//inteface code

 

// FlyBehavior.java
public   interface  FlyBehavior  {
    
public void Fly();
}

// QuakeBehavior.java
public   interface  QuakeBehavior  {
    
public void Quake();
}

// Duck.java
public   abstract   class  Duck  {
    FlyBehavior flyBehavior;
    QuakeBehavior quakeBehavior;
    
public void performFly()
    
{
        flyBehavior.Fly();
    }

    
public void performQuake()
    
{
        quakeBehavior.Quake();
    }

    
public abstract void Display();
    
public void SetFlyBehavior(FlyBehavior flyb)
    
{
        
this.flyBehavior = flyb;
    }

    
public void SetQuakeBehavior(QuakeBehavior quakeb)
    
{
        
this.quakeBehavior = quakeb;
    }

}

 

public   class  FlyWithWings  implements  FlyBehavior  {

    
public void Fly() {
        System.out.println(
"Fly with wings");
    }

}

public   class  FlyWithNoWings  implements  FlyBehavior  {
    
public void Fly() {
        System.out.println(
"Fly without wings");
    }


}

public   class  NormalQuake  implements  QuakeBehavior  {

    @Override
    
public void Quake() {
        System.out.println(
"Normal Quake");
    }


}

public   class  MuteQuake  implements  QuakeBehavior  {

    @Override
    
public void Quake() {
        
// TODO Auto-generated method stub
        System.out.println("Mute Quake");
    }


}

public   class  MallardDuck  extends  Duck  {

    @Override
    
public void Display() {
        
// TODO Auto-generated method stub
        System.out.println("I'm Mallard Duck");
    }


}

public   class  MiniDuckSimulator  extends  Duck  {

    @Override
    
public void Display() {
        
// TODO Auto-generated method stub
        System.out.println("I'm mini-duck");
    }


}

//display result

 

public   class  Main  {

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        Duck mallardDuck=new MallardDuck();
        Duck miniDuck
=new MiniDuckSimulator();
        
        FlyBehavior flyWithWings
=new FlyWithWings();
        FlyBehavior flyWithNoWings
=new FlyWithNoWings();
        QuakeBehavior normalQuake
=new NormalQuake();
        QuakeBehavior muteQuake
=new MuteQuake();
        
        
//Customize
        mallardDuck.SetFlyBehavior(flyWithWings);
        mallardDuck.SetQuakeBehavior(normalQuake);
        
        miniDuck.SetFlyBehavior(flyWithNoWings);
        miniDuck.SetQuakeBehavior(muteQuake);
        
        
//perfomance and display
        mallardDuck.Display();
        mallardDuck.performFly();
        mallardDuck.performQuake();
        miniDuck.Display();
        miniDuck.performFly();
        miniDuck.performQuake();
    }


}

Example2 (talk about PetShop4.0 developed by MS):

When inserting Order, PetShop System enable user customize which strategy he want to use, synchronization or asynchronization. i.e. the insert-order strategy is changeable at runtime, determined by user himself.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值