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的更多相关文章

  1. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  2. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

  3. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  4. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  5. 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 ...

  6. 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 ...

  7. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  8. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  9. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

随机推荐

  1. Spring Data Rest 支持Put请求

    最近在用 Spring Data Rest 写WebApi 遇到Put请求后报415 Mieda Type 的错误,GG了下 找到个解决办法,下面是针对Put请求的过滤. 1 public class ...

  2. ASP.NET Web API下的HttpController激活:程序集的解析

    ASP.NET Web API下的HttpController激活:程序集的解析 HttpController的激活是由处于消息处理管道尾端的HttpRoutingDispatcher来完成的,具体来 ...

  3. Centos 上使用mmsh协议听猫扑网络电台 VLC播放器

    Centos 上使用mmsh协议听猫扑网络电台 VLC播放器 安装CentOS已经有一段时间了,但是由于在Linux下除了学习,其他是事情都干不了.今天想闲来无事开了CentOS就想听一下歌,突然想起 ...

  4. 2013Esri全球用户大会之互操作和标准

    1:Esri在开源领域做过哪些工作? Esri一直以来就是开源技术的用户和支持者.我们相信,通过提供从上到下的开放平台可使我们的用户成为开发能力强大的解决方案的积极参与者.在现有技术形势下,我们正在将 ...

  5. jedis使用api

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 工作过程总结的一个示例,贴出来,如下: package com.wujintao.redis; import java.util.Da ...

  6. PB导出规定格式DBF文件

    最近在做一个给卫计委做数据上报的数据接口,接口要求使用奇葩的dBase 3数据库存储上报数据,忙活了几天总算搞好了,使用开发工具为powerbuild 12,222个字段的上报数据表生成DBF文件,写 ...

  7. LaTeX入门教程(二)

    LaTeX(LATEX,音译"拉泰赫")是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用 ...

  8. JSON反序列化实体类

    1.定义实体类 [DataContract] public class CustomerWordOrderViewModel { [DataMember] public string Name; [D ...

  9. js音乐播放器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat=&quo ...

  10. The Linux Mint 17.1:Eclipse Run The C++ And Python Configoration

    p { margin-bottom: 0.1in; line-height: 120% } # Copyright (c) 2016, 付刘伟 (Liuwei Fu)# All rights rese ...