今天介绍下策略模式,直接先上UML图

策略模式的概念

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、需要使用ConcreteStrategy提供的算法。
2、 内部维护一个Strategy的实例。
3、 负责动态设置运行时Strategy具体的实现算法。
4、负责跟Strategy之间的交互和数据传递。

策略模式的组成

— Strategy(抽象策略类): 通常由一个接口或者抽象类实现。
— ConcreteStrategy(具体策略角色):包装了相关的算法和行为。一般有多个
— Context(环境角色):持有一个策略类的引用,最终给客户端调用。

策略模式的应用场景

定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。策略模式使得算法可独立于使用它的客户而变化。实际开发中用途,比如google开源的android网络框架volley,volley的超时重发重发机制就使用到了典型策略模式,看源码
RetryPolicy.java 请求重试策略类,定义了三个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.android.volley;

/**
* 请求重试策略类
* 此类属于抽象策略类对应Strategy
*/
public interface RetryPolicy { /**
* 超时时间
*/
public int getCurrentTimeout(); /**
* 重试次数
*/
public int getCurrentRetryCount(); /**
* 针对错误异常的处理
*/
public void retry(VolleyError error) throws VolleyError;
}

DefaultRetryPolicy.java 默认重试策略,继承了RetryPolicy ,具体实现了接口,我截取了部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class DefaultRetryPolicy implements RetryPolicy {
...
public static final int DEFAULT_TIMEOUT_MS = 2500;
private int mCurrentTimeoutMs;
/** The default number of retries */
public static final int DEFAULT_MAX_RETRIES = 0; /** The default backoff multiplier */
public static final float DEFAULT_BACKOFF_MULT = 1f; ...//代表省略掉的代码 @Override
public int getCurrentTimeout() {
return mCurrentTimeoutMs;
} @Override
public int getCurrentRetryCount() {
return mCurrentRetryCount;
} @Override
public void retry(VolleyError error) throws VolleyError {
mCurrentRetryCount++;
mCurrentTimeoutMs += (mCurrentTimeoutMs * mBackoffMultiplier);
if (!hasAttemptRemaining()) {
throw error;
}
}
}

Request.java 请求类,持有一个抽象策略类RetryPolicy 的引用,最终给客户端调用。截取部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public abstract class Request<T> implements Comparable<Request<T>> {
private RetryPolicy mRetryPolicy; //持有策略类的引用 ... public Request(int method, String url, Response.ErrorListener listener) {
mMethod = method;
mUrl = url;
mIdentifier = createIdentifier(method, url);
mErrorListener = listener;
setRetryPolicy(new DefaultRetryPolicy()); //设置策略类 mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
}
... public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
mRetryPolicy = retryPolicy;
return this;
} ... public final int getTimeoutMs() {
return mRetryPolicy.getCurrentTimeout();
}
}

BasicNetwork.java 网络处理类,处理Request,使用到了对应的重试策略,截取部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
*遇到异常请求使用到了策略类的getTimeoutMs()方法
*/
private static void attemptRetryOnException(String logPrefix, Request<?> request,
VolleyError exception) throws VolleyError {
RetryPolicy retryPolicy = request.getRetryPolicy();
int oldTimeout = request.getTimeoutMs(); try {
retryPolicy.retry(exception);
} catch (VolleyError e) {
request.addMarker(
String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout));
throw e;
}
request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout));
} ... /**
* 网速慢,获取重试策略的重试次数getCurrentRetryCount
*/
private void logSlowRequests(long requestLifetime, Request<?> request,
byte[] responseContents, StatusLine statusLine) {
if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
VolleyLog.d("HTTP response for request=<%s> [lifetime=%d], [size=%s], " +
"[rc=%d], [retryCount=%s]", request, requestLifetime,
responseContents != null ? responseContents.length : "null",
statusLine.getStatusCode(), request.getRetryPolicy().getCurrentRetryCount());
}
} ... private static void attemptRetryOnException(String logPrefix, Request<?> request,
VolleyError exception) throws VolleyError {
RetryPolicy retryPolicy = request.getRetryPolicy();
int oldTimeout = request.getTimeoutMs(); try {
retryPolicy.retry(exception);
} catch (VolleyError e) {
request.addMarker(
String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout));
throw e;
}
request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout));
}

实际使用中,我们可以自定义自己的策略继承RetryPolicy,这样就可以按照我们自己请求策略执行。

下面介绍一个比较简单的实例

自定义简单的策略模式实例

商品实现针对不同等级会员显示对应的会员价,比如一本书原价100元,一级会员打97折,三级会员打8折

PriceStrategy.java,策略类,可以使抽象类或接口,定义算法的公共接口,

1
2
3
4
5
6
7
8
/**
* 定义一个计算价格的接口
* 它属于抽象策略类
* @author su
*/
public interface PriceStrategy {
double priceStrategyInterface(double price);
}

SuperVipStrategy.java,超级会员算法,打八折,继承PriceStrategy

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 超级会员类
* 它属于具体策略类,继承了计算价格接口
* @author su
*
*/
public class SuperVipStrategy implements PriceStrategy { @Override
public double priceStrategyInterface(double price) {
return price * 0.8; //打八折
}
}

OneVipStrategy.java,一级会员算法,打九七折,继承PriceStrategy

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 一级会员类
* 它属于具体策略类
* @author su
*
*/
public class OneVipStrategy implements PriceStrategy {
@Override
public double priceStrategyInterface(double price) {
return 0.97 * price; //打九七折
}
}

ThreeVipStrategy.java,三级会员算法,打九五折,继承PriceStrategy

1
2
3
4
5
6
7
8
9
10
11
/**
* 三级会员商品类
* 它属于具体策略类
* @author su
*/
public class ThreeVipStrategy implements PriceStrategy {
@Override
public double priceStrategyInterface(double price) {
return 0.95 * price; //九五折
}
}

Price.java 上下文环境,持有PriceStrategy 的引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 环境角色
* @author su
*
*/
public class Price {
private PriceStrategy priceStrategy;
public Price(PriceStrategy priceStrategy){
this.priceStrategy = priceStrategy;
}
public double getVipPrice(double price){
return priceStrategy.priceStrategyInterface(price);
}
}

测试实例

1
2
3
4
5
6
7
8
9
10
11
public class Test {
public static void main(String[] args){
SuperVipStrategy supVipSrategy = new SuperVipStrategy();
OneVipStrategy onVipStrategy = new OneVipStrategy();
System.out.print("超级会员价="+new Price(supVipSrategy).getVipPrice(100));
System.out.print("一级员价="+new Price(onVipStrategy).getVipPrice(100));
}
} -----输出结果---
超级会员价=80.0一级员价=97.0

实例的uml图

总结

优点

1、 提供了一种替代继承的方法,而且既保持了继承的优点(代码重用),还比继承更灵活(算法独立,可以任意扩展);
2、 避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展;
3、 遵守大部分GRASP原则和常用设计原则,高内聚、低偶合;
4、 易于进行单元测试,各个算法区分开,可以针对每个算法进行单元测试;

缺点

1、 因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量;
2、 选择何种算法需要客户端来创建对象,增加了耦合,这里可以通过与工厂模式结合解决该问题;
3、 程序复杂化。

Android设计模式之策略模式的更多相关文章

  1. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

  2. PHP设计模式之策略模式

    前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...

  3. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

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

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

  5. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

  6. Android 设计模式之MVC模式

    说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...

  7. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  8. [design-patterns]设计模式之一策略模式

    设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...

  9. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

随机推荐

  1. 多线程系列 线程池ThreadPool

    上一篇文章我们总结了多线程最基础的知识点Thread,我们知道了如何开启一个新的异步线程去做一些事情.可是当我们要开启很多线程的时候,如果仍然使用Thread我们需要去管理每一个线程的启动,挂起和终止 ...

  2. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  3. MongoDB (六) MongoDB 集合操作

    一. MongoDB 创建集合 createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 cr ...

  4. online judge 提交代码应该注意的事项

    首先,eclipse工程上出现红色的惊叹号,这个时候一般是工程的参考library或者build path的jar文件或者库文件缺失,可以右键工程,打开properties,点击 java build ...

  5. PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法

    PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法 PHP5.3 split() 不建议使用的原因:PHP 5.3.0 之后的r ...

  6. OpenStack学习系列-----第二篇 由一个错误看理解整个架构的重要性

    看了openstack没几天,然后就开始试着用Java调用所有的API,第一步得到Credentials的时候成功了,然后第二步,传参数使所有的server信息都列出来的时候报错404.具体描述如下( ...

  7. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

    一.简介 1.You now have three methods for distinguishing references:  Objects are identical if they occ ...

  8. 虚拟机WindowServer2003共享文件夹

    前话 之前我写过虚拟机装SQL Server的博文,也许有同学参考了也通过虚拟机装数据库服务来做实验作业了. 到了第二章节学习时,实验要求数据库导出Excel文件,这样一来就出现了小问题了,Windo ...

  9. Java基本开发环境搭建

    转http://blog.csdn.net/cxwen78/article/details/6400798 eclipse怎么编写JAVA,然后运行 1.新建一个Java project取个名字(首字 ...

  10. spry菜单栏(二)

    自定义选项卡式面板构件 尽管使用属性检查器可以简化对选项卡式面板构件的编辑,但是属性检查器并不支持自定义的样式设置任务.您可以修改选项卡式面板构件的 CSS 规则,并创建根据自己的喜好设置样式的构件. ...