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 ...
随机推荐
- wp7之换肤原理简单分析
wp7之换肤原理简单分析 纠结很久...感觉勉强过得去啦.还望各位大牛指点江山 百度找到这篇参考文章http://www.cnblogs.com/sonyye/archive/2012/03/12/2 ...
- 论公司spring的滥用
这个公司每个项目用不同的一套开发框架,实在忍不住拿一个出来说说事.
- JQuery实现分页程序代码
JQuery实现分页程序代码 做Web开发的程序员,分页时在所难免的,微软GridView.AspPager等设置分页数据可以自动分页,但是这里浏览器会闪动,用户体验不是很友好,在此我整理了JQuer ...
- IOS Objective-C 协议,委托
IOS Objective-C 协议,委托 IOS开发使用的语言Objective-C(以下简称OBJ-C)是一种扩展自C语言的面向对象语言.在OBJ-C中有一个很重要概念:消息.在最近的学习当中逐渐 ...
- Google photos -- reverse thinking
As a hacker, do the hacking. Here I mean the [hacker](http://en.wikipedia.org/wiki/Hacker_(term) ) . ...
- iOS之Xcode 8.0真机调试运行:This ** is running iOS 10.1.1 (14B100), which may not be supported
2016年10月份 苹果升级了iOS系统为10.1,xcode 8.0 运行会提示: This iPhone 5 (Model A1429) is running iOS 10.1.1 (14B100 ...
- linux面试题集锦3《转》
三.简答题: 1.简述Linux文件系统通过i节点把文件的逻辑结构和物理结构转换的工作过程. 参考答案: Linux通过i节点表将文件的逻辑结构和物理结构进行转换. i节点是一个64字节长的表,表中包 ...
- python 2.7 字符串处理
python 2 字符串处理小结: 字符替换:new_str = old_str.replace(char_old, char_new)--可用于清除字符串中所有的空格 字符分割(正/反):str_n ...
- google的作恶与不作恶
Google刚刚出现时,那时互联网还似桃花源,路不拾遗夜不闭户,最多升级升级病毒库.Google的发展,从商业模式上带来了广告对互联网无孔不入的渗透,如今Google.百度.阿里等各大巨头都有自己的广 ...
- hdu 5996 dingyeye loves stone(博弈)
题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...