策略设计模式 Strategy Design Pattern
来源:互联网 发布:qq mac 摄像头不能用 编辑:程序博客网 时间:2024/06/14 05:05
1. 策略设计模式初衷
减少代码冗余,降低代码之间的耦合度。同时保证代码的可维护性。
Positive:
- Often reduces long lists of conditions
- Avoid duplicate code
- Keep class changes from other class changes
- Can hide complicated/ secret code from the user
Negative:
Increased number of Objects/Classes
2. 策略实际模式应用场景
- 当我们想要定义一组算法,而且这些算法所要表现的的内容很相似时。如果在每个父类定义该算法的普适版本,然后在子类中覆盖override该方法这样一方面增加了代码的冗余度,另一方面违背了子类方法应该相互之间不同这一设计原则。
- 但我们想要动态改变对象的某种行为,且该行为有几种可供选择项时。
3. 具体UML图及解释
这里的Animal作为一个base class, derived class–包括Dog和Bird可以直接继承该父类,完成相应的功能,但这不是好的设计准则。因为这样引入了大量冗余代码,且违背了OOD的封装特性。
4. 一个具体的demo
package com.fqyuan.strategy;public interface Flys { public String fly();}class FlyFast implements Flys { @Override public String fly() { return "Flying fast!"; }}class FlySlow implements Flys { @Override public String fly() { return "Flying slowly!"; }}class NotFly implements Flys { @Override public String fly() { return "Unable to fly!"; }}package com.fqyuan.strategy;public class Animal { private String name; public Flys flyType; public void setName(String name) { this.name = name; } public String getName() { return name; } // The super class pushes off the responsibility for flying to flyType, an // interface instance public String tryToFly() { return flyType.fly(); } // We can add the method to change the Flys type dynamically public void setFlyType(Flys newFlyType) { flyType = newFlyType; } @Override public String toString() { return name; } /* * Bad, you don't want to add methods to the super class. You need to * separate what is different from subclass and the super class. */ // public void fly(){ // System.out.println("I'm flying!"); // }}package com.fqyuan.strategy;public class Dog extends Animal { public Dog(String name) { this.setName(name); /* * We set the interface polymorphically. Here we set the dog disability * to fly. */ flyType = new NotFly(); } /* * Bad! You can override the fly() method, but we're breaking the rule that * we need to abstract what is different to the subclasses. * 即:子类的方法尽量保持一般是和其他子类方法不同,这样可以减少代码的冗余度。 */ // public void fly() { // System.out.println("I can't fly!"); // }}package com.fqyuan.strategy;public class Cat extends Animal { public Cat(String name) { setName(name); this.flyType = new FlyFast(); }}package com.fqyuan.strategy;/* * 设计模式出现的缘由? * 代码的可读性,可维护性,减少代码的冗余度. * 2条重要原则: * 1). 能使用composition的尽量不要用inheritance. * 2). 能使用Interface的尽量不要用具体的class. */public class StrategyDemo { public static void main(String[] args) { Animal dog = new Dog("Rick"); Cat cat = new Cat("Kitty"); System.out.println(dog + " " + dog.tryToFly()); System.out.println(cat + " " + cat.tryToFly()); dog.setFlyType(new FlySlow()); System.out.println(dog + " " + dog.tryToFly()); }}//Running result:Rick Unable to fly!Kitty Flying fast!Rick Flying slowly!
阅读全文
0 0
- Strategy Design Pattern 策略设计模式
- 策略设计模式 Strategy Design Pattern
- Java设计模式 Design Pattern:策略模式 Strategy Pattern
- Design Pattern-Strategy Pattern 设计模式之策略模式
- Java设计模式 Design Pattern:策略模式 Strategy Pattern
- 设计模式1--策略模式(the strategy design pattern)
- 设计模式学习—策略模式(Strategy Design Pattern)
- 8.2.1 策略设计模式(The strategy design pattern)
- 策略模式 in Java (Strategy Design Pattern)
- Java Design Patterns--Strategy Pattern --策略模式
- Design Pattern 设计模式1 - Strategy 1
- 设计模式 - Strategy Pattern(策略模式)
- 设计模式 - Strategy Pattern(策略模式)
- 设计模式20:Strategy Pattern (策略模式)
- 设计模式-策略模式(Strategy Pattern)
- 设计模式:策略模式 strategy pattern
- java设计模式---Strategy Pattern---策略模式
- 设计模式之策略模式(Strategy Pattern)
- Android解决65535限制
- 你怎么理解ioc/DI?
- valign和vertical-align区别
- 3
- F1V2.x-图形-离线地图使用
- 策略设计模式 Strategy Design Pattern
- Linux: could not open lock file /var/lib/dpkg/lock -open (13: Permission denied)
- woff2文件找不到的问题
- 二维树状数组
- 设计模式(1)单例模式
- Windows + Faster R-CNN配置若干问题与解决
- web.xml中关于Servlet、Filter、Listener的配置
- Android--获取当前系统的语言环境
- 简单入门循环神经网络RNN:时间序列数据的首选神经网络