Groovy 设计模式 -- Strategy 模式
策略模式
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 模式的更多相关文章
- C++设计模式---Strategy模式
一.前言 学习的第一个设计模式!不知道理解的对不对,期望大家一起多交流~ Strategy模式:策略模式,定义了算法族,分别封装起来,此模式可以让算法的变化独立于使用算法的客户.Strategy模式将 ...
- Groovy 设计模式 -- 迭代器模式
Iterator Pattern http://groovy-lang.org/design-patterns.html#_flyweight_pattern 迭代器模式,允许顺序访问 聚集对象中的中 ...
- Groovy 设计模式 -- 保镖模式
Bouncer Pattern http://groovy-lang.org/design-patterns.html#_bouncer_pattern 保镖模式主要负责对函数的输入参数的合法性检查, ...
- android 开发设计模式---Strategy模式
假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等.而如果不使用任何模式,我们的代码可能就是这样子的. 12345678910111213141516171819202122 ...
- Groovy 设计模式 -- 组合模式
Composite Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 组合模式, ...
- 设计模式-策略模式(Strategy Model)
1.概述 在开发过程中常常会遇到类似问题,实现一个功能的时候往往有多种算法/方法(策略),我们可以根据环境的不同来使用不同的算法或策略来实现这一功能. 如在人物比较排序的实现中,我们有 ...
- 策略模式设计模式(Strategy)摘录
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例.一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将 ...
- [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型)
[.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模 ...
- Java设计模式(18)策略模式(Strategy模式)
Strategy是属于设计模式中 对象行为型模式,主要是定义一系列的算法,把这些算法一个个封装成单独的类. Stratrgy应用比较广泛,比如,公司经营业务变化图,可能有两种实现方式,一个是线条曲线, ...
随机推荐
- poj1845 sumdiv (因数的和)
首先分解质因数,$A^B=p_1^{m_1B}p_2^{m_2B}...p_n^{m_nB}$ 然后的话,它的所有因数的和就是$\prod{(1+p_i^1+p_i^2+...+p_i^n)}$ 用一 ...
- NOI2010能量采集(数学)
栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得非常整齐,一共有n列,每 ...
- Jupyter-Notebook服务器自定义密码
往期回顾 Anaconda安装:https://www.cnblogs.com/dotnetcrazy/p/9158715.html 基本知识导航篇:https://www.cnblogs.com/d ...
- Max Mex
Max Mex 无法直接处理 可以二分答案! [0,mid]是否在同一个链上? 可以不修改地做了 修改? 能不能信息合并?可以! 记录包含[l,r]的最短链的两端 可以[0,k][k+1,mid]合并 ...
- A1120. Friend Numbers
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- 蛋白质结构模型和功能预测:Swiss-model工具的使用
Swiss-model也是一款预测蛋白质结构模型的工具.网页地址:https://swissmodel.expasy.org/ 首先,进行常规的注册后,点击start modelling 以搜索BRC ...
- redis设置最大内存
- 第十三节,卷积神经网络之经典网络LeNet-5、AlexNet、VGG-16、ResNet(三)(后面附有一些网络英文翻译文章链接)
一 实例探索 上一节我们介绍了卷积神经网络的基本构建,比如卷积层.池化层以及全连接层这些组件.事实上,过去几年计算机视觉研究中的大量研究都集中在如何把这些基本构件组合起来,形成有效的卷积神经网络.最直 ...
- HOG特征(Histogram of Gradient)学习总结
最近在做的项目有用到HOG+SVM这一方面的知识,参考相关论文和网上一些博文在此对HOG特征进行下总结. 参考资料: HOG的经典论文:Dalal N, Triggs B. Histograms of ...
- appium desktop 1.7 byName不能用,重写
@Override public WebElement findElementByName(String name){ String string="new UiSelector().tex ...