Design Pattern - Strategy
Strategy Pattern:
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.
第一个设计原则
找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起,把会变化的部分取出并“封装”起来,好让其他部分不受影响。从而使变化引起的不经意后果变少,系统更有弹性,实现对改变关闭对扩展开放的原则。在实际的编码过程中可以随心所遇的使用多态,继承,封装三大特性。
第二个设计原则
针对接口编程而不是针对实现编程。
第三个设计原则
多用组合,少用继承。
策略模式组成:
策略模式的应用场景:
1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。
2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。
3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。
具体应用案例
package cn.com.dp.characters; import cn.com.dp.imp.Weapon;
public abstract class Character {
//Character有一个weapon接口对应的行为
private Weapon weapon; public Weapon getWeapon() {
return weapon;
} public void setWeapon(Weapon weapon) {
this.weapon = weapon;
} public abstract void fight();
public abstract String selfInfo();
}
package cn.com.dp.characters; /**
* 角色:郭嘉
*
* @author cpt
*
*/
public class GuoJia extends Character {
@Override
public void fight() {
System.out.print(selfInfo());
super.getWeapon().attack();
} @Override
public String selfInfo() {
return "呵呵,就这样吧....";
}
}
package cn.com.dp.characters; /**
* 角色:贾诩
* @author cpt
*
*/
public class JiaXu extends Character{ @Override
public void fight() {
System.out.print(selfInfo());
getWeapon().attack();
} @Override
public String selfInfo() {
return "我能完杀!!!";
} }
package cn.com.dp.imp;
/**
* 所有武器攻击效果接口
* @author cpt
*
*/
public interface Weapon {
void attack();
}
package cn.com.dp.impl; import cn.com.dp.imp.Weapon;
/**
* 诸葛连弩攻击效果,可复用替换
* @author cpt
*
*/
public class AK47 implements Weapon { @Override
public void attack() {
System.out.println("AK47连续攻击效果");
} }
package cn.com.dp.impl; import cn.com.dp.imp.Weapon;
/**
* 麒麟弓攻击效果,可复用替换
* @author cpt
*
*/
public class UnicornBow implements Weapon { @Override
public void attack() {
System.out.println("麒麟弓命中射马攻击效果");
} }
package cn.com.dp.context; import cn.com.dp.characters.GuoJia;
import cn.com.dp.characters.JiaXu;
import cn.com.dp.impl.AK47;
import cn.com.dp.impl.UnicornBow; public class StrategyTestContext {
public static void main(String[] args) {
AK47 ak47 = new AK47();
UnicornBow unicornBow=new UnicornBow(); GuoJia guoJia = new GuoJia();
guoJia.setWeapon(ak47);
guoJia.fight(); JiaXu jiaXu = new JiaXu();
jiaXu.setWeapon(unicornBow);
jiaXu.fight(); jiaXu.setWeapon(ak47);
jiaXu.fight();
}
}
关于策略模式能不能消除if else判断结构,个人认为策略模式是做不到。
http://programmers.stackexchange.com/questions/146761/design-pattern-for-handling-a-response
http://industriallogic.com/xp/refactoring/conditionalWithStrategy.html
个人对策略模式理解总结:
所有的设计模式并不是为了为了约定编程规范,而是一种总结下来针对特定需求的设计模式而绝不是编程模式,完全没有必要刻意去迁就设计模式,而且设计模式并不是什么高深的技术不过是巧妙地运用Java特性对代码进行解耦。但是了解各个模式的设计技巧核心本质对以后设计项目以及编写模块代码会有很大的帮助。策略模式巧妙的地方在于把应用中一类相似的变化的行为代码封装成可复用独立于执行环境的组件供策略环境角色随意组合调用,如果变化的行为代码不可复用那么就没有必要使用策略模式。
Design Pattern - Strategy的更多相关文章
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- C++ Design Pattern: What is a Design Pattern?
Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when deve ...
- Design Pattern in Simple Examples
Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
随机推荐
- 该死的类型转换For input string: "[Ljava.lang.String;@1352dda"
今天又遇见了这个该死的问题,还是记下来备忘. 从map里取值的时候,将OBJECT对象 先转换成String 然后转换成integer报错 java.lang.NumberFormatExceptio ...
- Go语言Web框架gwk介绍4
Go语言Web框架gwk介绍 (四) 事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(m ...
- JavaEE:Tomcat服务器常用配置和HTTP简介
Web服务器常用配置1.Web系统采用B/S结构通信的:Browser --- Server1)浏览器向服务器发送访问目标资源请求(请求)2)服务器根据请求的目标资源路径,在服务器端进行查找(请求查找 ...
- Arduino 3G shield using SoftwareSerial to control
On the 3G shield, by default the power pin is on D8 and reset pin is on D9. Make it HIGH then it wor ...
- 百度地图在某架构下找不到符号.a文件的问题
1.现象: 就是说找不到符号给i386的架构(就是模拟器).或者找不到符号给arm架构(真机). ld: warning: ignoring file /Users/pufang/xcode/demo ...
- 第2章 开始入手 —— 01 创建第一个 Android 应用程序
创建一个新的 Android 项目 操作步骤: (1) 选择 File | New | Android Application Project ,弹出 New Android Application ...
- 使用Unity创建塔防游戏(Part1)
How to Create a Tower Defense Game in Unity - Part1 原文作者:Barbara Reichart 文章原译:http://www.cnblogs.co ...
- SpringMVC第一天(其他)
SpringMVC第一天 框架课程 课程计划 参数绑定 SpringMVC默认支持的类型 简单数据类型 Pojo类型 Pojo包装类型 自定义参数绑定 SpringMVC和Struts2的区别 高级参 ...
- ASP文件上传代码
在网上看到的代码,稍微有点问题,改了一下就可以了.Chrome下是可以用的,别的浏览器还没有确认. <% Response.Buffer = True Server.ScriptTimeOut= ...
- 快速理解Parquet的DL和RL
关于Parquet的详细介绍,请参考: 新一代列式存储格式Parquet,此文中对Parquet做了详细的介绍,这里不做重复介绍,但其中关于Definition Level(DL)和Repeated ...