设计模式之策略者模式(Strategy)

The Strategy Pattern 定义了算法族, 分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户

It started with a simple duck app~!
Case description:
Joe works for a company that makes a highly successful duck pond simulation game, SimUDuck. The game can show a large variety of duck species swimming and making quacking sounds.The initial designers of the system used standard OO techniques and created on Duck superclass from which all other duck types inherit.
Class diagram description:
first we create a Duck class, it is abstarct.
这里写图片描述
Ok~ if your company executives think it’s time for a big innovation to your Duck app, make it can fly for part of the duck type . you should not failed to notice that not all subclasses of Duck should fly. so according to the above design. if we add a new behavior that was not appropriate for some Duck subclasses. it’ll course jokes ! because all the subclasses will inherit his superclasses. so we use inheritance for the purpose of reuse turns out so well when it comes to maintenance.
Wouldn’t it be dreamy if only there were a way to build software so that when we need to change it , we could do it with the least possible impact on the existing code ? We could spend less time reworking code and more making the program do cooler things…
No matter where you work, what you’re buiding, or what language you are programming in, what’s the one true constant that will be with you always? that’s CHANGE!
Design Principle 一、
找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混在一起
take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t.
Design Principle 二、
针对接口编程,而不是实现。 Program to an interface , not an implementation
now we depart fly behavior and quack behavior from the Duck. we’ll use an interface to represent each behavior, for instance, FlyBehavior and QuackBehavior ..and each implementation of a behavior will implement one of those interfaces.
That way, the Duck classes won’t need to know any of the implementation details for their own behaviors.
Now:
这里写图片描述
with this design, other types of objects can reuse our fly and quack behaviors because these behaviors are no longer hidden away in our Duck classes !
And we can add new behaviors without modifying any of our existing behavior classes or touching any of the Duck classes that use flying behaviors. So we get the benefit of REUSE without all the baggage that comes along with inheritance.
Duck:

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public abstract class Duck {
    public FlyBehavior flyBehavior;
    public QuackBehavior quackBehavior;
    public Duck(){

    }

    public abstract void display();

    public void performFly() {
        flyBehavior.fly();
    }

    public void performQuack() {
        quackBehavior.quack();
    }

    public void swim() {
        System.out.println("All the ducks can fly");
    }

    public void  setFlyBehavior(FlyBehavior flyBehavior) {
        this.flyBehavior = flyBehavior;
    }

    public void setQuackBehavior(QuackBehavior quackBehavior) {
        this.quackBehavior = quackBehavior;
    }
}

we can set the duck’s behavior type through a setter method on the duck subclass, rather than instantiating it in the duck’s constructor.

MallardDuck:

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public class MallardDuck extends Duck{

    public MallardDuck() {
        quackBehavior = new Quack();
        flyBehavior = new FlyWithWings();
    }
    @Override
    public void display() {
        System.out.println("I hava my own display methods~!");
    }

    public static void main(String[] args) {
        MallardDuck mallardDuck = new MallardDuck();
        mallardDuck.performFly();
        mallardDuck.performQuack();
        mallardDuck.display();
        System.out.println("now we want to our duck be powered by rocket~!");
        mallardDuck.setFlyBehavior(new FlyRocketPowered());
        mallardDuck.performFly();
        mallardDuck.performQuack();
        mallardDuck.display();
    }
}

To change a duck’s behavior at runtime, just class the duck’s setter method for that behavior.
FlyBehavior

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public interface FlyBehavior {
    public void fly();
}

QuackBehavior

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public interface QuackBehavior {
    public void quack();
}

Quack

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public class Quack implements QuackBehavior {
    @Override
    public void quack() {
        System.out.println("I can quack ~!");
    }
}

FlyWithWings

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public class FlyWithWings implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I can fly with wings !");
    }
}

FlyRocketPowered

/**
 * CopyRright (c)2014-2016 Haerbin Hearglobal Co.,Ltd
 * Project: idoc-main
 * Comments:
 * Author:cbam
 * Create Date:2016/11/17
 * Modified By:
 * Modified Date:
 * Modified Reason:
 */
public class FlyRocketPowered implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I'm flying with a rocket !");
    }
}

运行结果:
这里写图片描述
Class diagram:
这里写图片描述

Design Principle三
多用组合,少用继承 (Favor composition over inheritance)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值