简介

策略属于行为型模式的一种,策略模式允许对象的行为或算法在运行时改变,使用不同的算法达成相同的结果或目的.

实现层面上,定义一个抽象的算法接口, 然后根据具体算法的不同定义不同的类去实现该接口, 调用时不同实现的实例可以相互替换.

策略模式适用于多个类只有行为不同,运行时根据上下文决定使用哪种行为; 根据实际情况不同算法可能不同, 或算法的最优实现在未来确定.

意图

定义一系列算法,封装每个算法,并使它们可以互换。

类图

实现

以绝地求生游戏为例, 要想赢得最后的比赛可能由多种方式(策略), 抽象出 策略接口 WinStrategy, 实现策略 比如激进点的方式AlwaysKillWinStrategy,猥琐点的方式AlwaysHideWinStrategy.

下面我们将实现策略模式(一,二,三步), 并最后讨论更具实际意义的策略与上下文通信的两种方式及其各自利弊(四).

一. 定义策略接口

/**
* 赢得吃鸡的策略接口
*/
public interface WinStrategy {
void win();
}

二. 实现策略,使用两种不同的算法实现

/**
* 采用人挡杀人的模式
*/
public class AlwaysKillWinStrategy implements WinStrategy {
public void win() {
System.out.println("总是很钢,大杀四方");
}
} /**
* 采用LYB的模式
*/
public class AlwaysHideWinStrategy implements WinStrategy {
public void win() {
System.out.println("总是很苟,活得长久");
}
}

三. 调用

public class StrategyTest {
public static void main(String[] args) {
WinStrategy iWillWin = new AlwaysHideWinStrategy();
iWillWin.win();
iWillWin = new AlwaysKillWinStrategy();
iWillWin.win();
} }

四. 策略与上下文

上面的例子中我们并没有体现出上下文的概念, 其隐藏的上下文在main()函数中. 除了 策略接口 和 实现策略 外, 还需要一个 策略上下文 角色.一般由上下文持有策略的引用, 并对策略进行实际调用,下面的代码定义该角色

/**
* 上下文
*/
public class Context { private String weather;
WinStrategy winStrategy; public Context(String weather, WinStrategy winStrategy) {
this.weather = weather;
this.winStrategy = winStrategy;
} public void winGame() {
winStrategy.win();
System.out.println("天气情况:"+this.weather+"; 采用的策略:"+winStrategy.getClass());
} } //调用
public class StrategyTest {
public static void main(String[] args) {
WinStrategy iWillWin = new AlwaysHideWinStrategy();
Context context = new Context("雾天",iWillWin);
context.winGame();
}
}

策略中有时候也需要某些上下文信息, 例如win()方法需要游戏场景中的天气情况, 这种情况下可以将上下文引用传入策略, 代码修改如下:

/**
* 赢得吃鸡的策略接口,上下文作为参数传入
*/
public interface WinStrategy {
void win(Context context);
} /**
* 采用LYB的模式, 打印出天气情况
*/
public class AlwaysHideWinStrategy implements WinStrategy {
public void win(Context context) {
System.out.println("总是很苟,活得长久; 天气: "+context.getWeather());
}
} /**
* 上下文
*/
@Data
public class Context { private String weather;
WinStrategy winStrategy; public Context(String weather, WinStrategy winStrategy) {
this.weather = weather;
this.winStrategy = winStrategy;
} public void winGame() {
winStrategy.win(this);
System.out.println("天气情况:"+this.weather+"; 采用的策略"+winStrategy.getClass());
} }

上面通过将上下文引用作为参数传递给策略来实现了策略与上下文的通信, 对于不同策略的实现,未必都会用到上下文中的全部信息, 这种情况下传递上下文引用成为了一种"浪费". 因此有的人更愿意将使用的信息作为属性定义在策略实现类中,实现更有针对性的代码

两种方式各有利弊, 传递上下文引用的方式利于扩展, 且可以做到风格统一, 但可能会导致"浪费", 如果扩展太多上下文子类不利于理解. 注入的方式实现在策略类中方式比较简单, 容易实现, 但也会导致策略与其他策略风格不一致.

/**
* 赢得吃鸡的策略接口
*/
public interface WinStrategy {
void win();
} /** * 采用人挡杀人的模式
*/
@AllArgsConstructor
public class AlwaysKillWinStrategy implements WinStrategy { private String weather; public void win() {
System.out.println("总是很钢,大杀四方"+" 天气:"+weather);
}
} public class StrategyTest {
public static void main(String[] args) {
WinStrategy iWillWin = new AlwaysKillWinStrategy("雾天");
Context context = new Context("雾天",iWillWin);
context.winGame();
}
}

总结

优点: 1. 易于扩展;2.避免了if...else... 3. 代码重用

缺点: 调用方需要知道使用的哪种算法, 算法需要完全暴露; 可能会导致实现类过多;

设计模式七: 策略(Strategy)的更多相关文章

  1. C++设计模式实现--策略(Strategy)模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/30489331 一. 举例说明 曾经做了一个程序,程序 ...

  2. Java设计模式之 — 策略(Strategy)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8986285 今天你的leader兴致冲冲地找到你,希望你可以帮他一个小忙,他现在急 ...

  3. Head First 设计模式 - 01. 策略 (Strategy) 模式

    当涉及到"维护"时,为了"复用"目的而使用继承,结局并不完美 P4 对父类代码进行修改时,影响层面可能会很大 思考题 利用继承来提供 Duck 的行为,这会导致 ...

  4. 设计模式 策略-Strategy,装饰-Decorator,观察者-Observer

    重温了Head First 3个设计模式.提炼一下思想,笔记如下. 策略-Strategy 当一个类或类族中重复实现某些同类的方法(behavior)时,考虑使用策略模式. 该模式是将Behavior ...

  5. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  6. 设计模式C++实现(1)——策略(Strategy)模式

    目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...

  7. Strategy Pattern ava设计模式之策略模式

    简介 在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式.简单理解就是一组算法,可以互换,再简单点策略就是封装算法. 意图 定义一 ...

  8. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  9. 设计模式之策略模式和状态模式(strategy pattern & state pattern)

    本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...

随机推荐

  1. Linux之指令 重定向 文件覆盖>和文件追加>>

    指令>和>>区别 指令 > : 如果文件存在,将原来文件的内容覆盖:原文件不存在则创建文件,再添加信息. 指令 >>:不会覆盖原文件内容,将内容追加到文件的尾部. ...

  2. Vmware10中Centos7挂载Windows主机的共享文件夹,提示:Error: cannot mount filesystem: No such device

    1.设置共享权限 2.安装VMware tools 点击虚拟机 点击安装 VMware tools 将/run/media/zhaojq/VMware\ Tools 目录下的VMwareTools-9 ...

  3. springMVC第二天——高级参数绑定与其它特性

    大纲摘要: 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 2.@RequestMapping注解的使用 3.Controller方法返回值 4.Springmvc中异常处理 5 ...

  4. sub2ind函数

    sub2ind函数是MATLAB中对矩阵索引号检索的函数,用法如下: >> A=rand(5) A = 0.6850    0.0867    0.2290    0.1006    0. ...

  5. 老铁啊,我同你讲, 这年头不会点 Git 真不行!!!

    -------------------------------------知识是一点一点的积累的, 也是一点一点的吸收的,没有人一口就能吃成一个胖子. 版本控制 说到版本控制,脑海里总会浮现大学毕业是 ...

  6. Linux Crontab Shell脚本实现秒级定时任务

    一.编写Shell脚本crontab.sh #!/bin/bash step=1 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do $( ...

  7. Flask —— 信号(5)

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. pip3 install blinker 1. 内置信号 request_started = ...

  8. MINIST深度学习识别:python全连接神经网络和pytorch LeNet CNN网络训练实现及比较(三)

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 在前两篇文章MINIST深度学习识别:python全连接神经网络和pytorch LeNet CNN网 ...

  9. 6-4 The present perfect

    1 Summary The present perfect is an important verb tense in English. It is used to talk about things ...

  10. LeetCode-876 链表的中间结点

    对于链表的中某个位置结点的定位一般都会用到两个链表结点指针,例如链表倒数第K个结点问题使用的是先后指针,该题中用到的快慢指针. 本题的具体解法就是快指针走两步.慢指针走一步知道遍历完结点,重点是分清题 ...