设计模式-01-策略模式
原文链接 https://demon7452.github.io/2016/09/28/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-01-%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。
策略模式-Strategy Pattern
GitHub源码
定义
The Strategy Pattern defines a family of algorithms(演算法;运算法则;),encapsulates( 总结; 扼要概括; ) each one,and makes them interchangeable(可交换的;可交替的;).Strategy lets the algorithm vary independently from clients that use it.
策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
设计原则 Design Principle
- Identify(识别,认出;) the aspects(方面) of your application that vary(变化;不同) and separate(分开;分离; )them from what stays the same.
- 找出你的应用中会发生变化的部分,并将它们与不变的部分分离开来。
- Program to an interface,not an implementation.
- 针对接口编程,而不是针对实现编程。
- Favor(支持;赞成;) composition(组合) over inheritance(继承).
- 优先使用组合而非继承
使用实例-Duck
Structure-设计结构
Code-代码实现
所有鸭子的超类,将所有的鸭子相同的方法swim和display方法放在超类中,不同的方法fly和quack用接口实现,超类中定义接口变量。
/**
*
* Function: supper class duck
* 鸭子的超类
*
* @author admin
* @Date 2016年9月30日 下午4:48:05
*
* @see
*/
public abstract class Duck {
private FlyInterface flyInterface;//动作类接口,提供setter and getter
private QuackInterface quackInterface;//动作类接口,提供setter and getter
public Duck(){
}
public Duck(FlyInterface flyInterface,QuackInterface quackInterface){
this.flyInterface = flyInterface;
this.quackInterface = quackInterface;
}
public abstract void display();
public void swim(){
System.out.println("all ducks float,even decoys!!");
}
public void performFly(){
flyInterface.fly();
}
public void performQuack(){
quackInterface.quack();
}
}
/**
* sub class mallard duck 绿头鸭
*/
public class MallardDuck extends Duck {
/**
* 该构造函数保证每个鸭子实例类必须为接口对象赋予实例对象
* 保证父类调用接口方法时不会出现空指针异常
* Creates a new instance of MallardDuck.
* @param fly
* @param quack
*/
public MallardDuck(FlyInterface fly,QuackInterface quack){
super(fly,quack);
}
@Override
public void display() {
System.out.println("I am a mallard duck(绿头鸭)");
}
}
fly()和quack()方法的接口和实现
/**
* interface
*/
public interface FlyInterface {
public void fly();
}
/**
* implementation
*/
public class FlyNoWay implements FlyInterface {
@Override
public void fly() {
System.out.println("I can not fly!");
}
}
public class FlyWithWings implements FlyInterface {
@Override
public void fly() {
System.out.println("I can fly with wings,hahaha");
}
}
//QuackInterface 接口略
test case
public class TestStrategyPattern {
public static void main(String[] args) {
Duck mallarDuck = new MallardDuck(new FlyWithWings(),new QuackMute());
mallarDuck.display();
mallarDuck.swim();
mallarDuck.performFly();
mallarDuck.performQuack();
mallarDuck.setFlyInterface(new FlyNoWay());
mallarDuck.performFly();
}
}
设计模式高大上的好处
Design patterns give you a shared vocabulary with other developers. Once you've got the vocabulary you can more easily communicate with other developers and inspire those who don't know patterns to start learning them. It also elevates(提高; 提升;) your thinking about architectures by letting you think at the pattern level,not the nitty gritty(繁琐的) object level.