策略模式

https://en.wikipedia.org/wiki/Strategy_pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. The strategy pattern

  • defines a family of algorithms,
  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family.

Strategy lets the algorithm vary independently from clients that use it.[1] Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software.

The Strategy [2] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Strategy design pattern solve? [3]

  • A class should be configured with an algorithm instead of implementing an algorithm directly.
  • An algorithm should be selected and exchanged at run-time.

What is an algorithm? An algorithm is usually defined as a procedure that takes some value as input, performs a finite number of steps, and produces some value as output.
From a more general point of view, an algorithm is a piece of code that does something appropriate.

EXPLAIN

http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

在阎宏博士的《JAVA与模式》一书中开头是这样描述策略(Strategy)模式的:

  策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。


策略模式的结构

  策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理。策略模式通常把一个系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。用一句话来说,就是:“准备一组算法,并将每一个算法封装起来,使得它们可以互换”。下面就以一个示意性的实现讲解策略模式实例的结构。

  这个模式涉及到三个角色:

  ●  环境(Context)角色:持有一个Strategy的引用。

  ●  抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。

  ●  具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。

DEMO

https://en.wikipedia.org/wiki/Strategy_pattern

import java.util.ArrayList;
import java.util.List; public class StrategyPatternWiki { public static void main(final String[] arguments) {
Customer firstCustomer = new Customer(new NormalStrategy()); // Normal billing
firstCustomer.add(1.0, 1); // Start Happy Hour
firstCustomer.setStrategy(new HappyHourStrategy());
firstCustomer.add(1.0, 2); // New Customer
Customer secondCustomer = new Customer(new HappyHourStrategy());
secondCustomer.add(0.8, 1);
// The Customer pays
firstCustomer.printBill(); // End Happy Hour
secondCustomer.setStrategy(new NormalStrategy());
secondCustomer.add(1.3, 2);
secondCustomer.add(2.5, 1);
secondCustomer.printBill();
}
} class Customer { private List<Double> drinks;
private BillingStrategy strategy; public Customer(final BillingStrategy strategy) {
this.drinks = new ArrayList<Double>();
this.strategy = strategy;
} public void add(final double price, final int quantity) {
drinks.add(strategy.getActPrice(price*quantity));
} // Payment of bill
public void printBill() {
double sum = 0;
for (Double i : drinks) {
sum += i;
}
System.out.println("Total due: " + sum);
drinks.clear();
} // Set Strategy
public void setStrategy(final BillingStrategy strategy) {
this.strategy = strategy;
} } interface BillingStrategy {
double getActPrice(final double rawPrice);
} // Normal billing strategy (unchanged price)
class NormalStrategy implements BillingStrategy { @Override
public double getActPrice(final double rawPrice) {
return rawPrice;
} } // Strategy for Happy hour (50% discount)
class HappyHourStrategy implements BillingStrategy { @Override
public double getActPrice(final double rawPrice) {
return rawPrice*0.5;
} }

Groovy 设计模式 -- Strategy 模式的更多相关文章

  1. C++设计模式---Strategy模式

    一.前言 学习的第一个设计模式!不知道理解的对不对,期望大家一起多交流~ Strategy模式:策略模式,定义了算法族,分别封装起来,此模式可以让算法的变化独立于使用算法的客户.Strategy模式将 ...

  2. Groovy 设计模式 -- 迭代器模式

    Iterator Pattern http://groovy-lang.org/design-patterns.html#_flyweight_pattern 迭代器模式,允许顺序访问 聚集对象中的中 ...

  3. Groovy 设计模式 -- 保镖模式

    Bouncer Pattern http://groovy-lang.org/design-patterns.html#_bouncer_pattern 保镖模式主要负责对函数的输入参数的合法性检查, ...

  4. android 开发设计模式---Strategy模式

    假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等.而如果不使用任何模式,我们的代码可能就是这样子的. 12345678910111213141516171819202122 ...

  5. Groovy 设计模式 -- 组合模式

    Composite Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 组合模式, ...

  6. 设计模式-策略模式(Strategy Model)

    1.概述     在开发过程中常常会遇到类似问题,实现一个功能的时候往往有多种算法/方法(策略),我们可以根据环境的不同来使用不同的算法或策略来实现这一功能.     如在人物比较排序的实现中,我们有 ...

  7. 策略模式设计模式(Strategy)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例.一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将 ...

  8. [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型)

    [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模 ...

  9. Java设计模式(18)策略模式(Strategy模式)

    Strategy是属于设计模式中 对象行为型模式,主要是定义一系列的算法,把这些算法一个个封装成单独的类. Stratrgy应用比较广泛,比如,公司经营业务变化图,可能有两种实现方式,一个是线条曲线, ...

随机推荐

  1. html概述和基本结构

    html概述 HTML是 HyperText Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的语言,这种语言由一个个的标签 ...

  2. Yii2 如何输出 sql 语句?

    可以用 $model->find()->createCommand()->getRawSql(); 不能带 all(). find() 它的 返回值类型是 yii\db\Active ...

  3. java ee Concurrency 并发编程

    https://www.javacodegeeks.com/2014/07/java-ee-concurrency-api-tutorial.html This is a sample chapter ...

  4. 洛谷P3354 河流

    有点权边权的树,选出k个关键点,根必须选.每个点的贡献为点权 * 到最近的关键祖先的距离.求最小总贡献. 解:树形DP是最毒瘤的算法...... 设fxij表示以x为根的子树中选了j个关键点,且x的最 ...

  5. 【洛谷P4144】大河的序列

    题目大意:给定一个长度为 N 的序列,求序列中连续区间最大的(或和加与和)是多少. 题解: 引理:任意两个数 \(i, j\),若 \(i>j\),则在二进制表示下,i 对应的二进制串的字典序一 ...

  6. (转)visual stdio 书签功能介绍

    http://www.mycode.net.cn/tools/1615.html 使用 Visual Studio 开发过程中,你很容易遇到一种情况就是多个文件来回的切换,在每一块实现不同的业务,打开 ...

  7. 利用SHAPEIT将vcf文件进行基因型(genotype)定相(phasing):查看两个突变是否来源于同一条链(染色体或父本或母本),two mutations carried by the same read

    首先,下载SHAPEIT. 按照里面的步骤安装完后,将vcf文件进行基因型定相,分四步走. 第一步,将vcf文件转化为plink二进制文件(.bed, .bim, .fam). 这一步需要用到GATK ...

  8. Oracle 的常用概念

    SQL优化(数据库的优化) 1. 尽量使用列名(不用*) 2. where解析顺序: 右--> 左 3. 自连接不适合操作大表 4. 尽量使用多表查询不使用子查询语句 5. 尽量不要使用集合运算 ...

  9. halcon形态学运算

    腐蚀.膨胀.开运算(先腐蚀后膨胀).闭运算(先膨胀后腐蚀) 以halcon自带案例ball.hdev为例(开运算),注意代码有所缩减.关于sort_region函数,参考http://blog.sin ...

  10. sprignclou的zuul的实战

    idea启动集群服务: https://blog.csdn.net/love_dl_forever/article/details/78918181 http://www.scienjus.com/a ...