策略模式(Strategy)---行为型
1 基础知识
定义:定义了算法家族,分别封装起来,让他们可以相互替换,此模式让算法的变化不会影响到使用算法的用户(应用层)。特征:可以替换掉大量的if else语句
本质:分离算法,选择实现。
使用场景:
(1)出现有许多相关的类,仅仅是行为有差别的情况下,可以使用策略模式来使用多
个行为中的一个来配置一个类的方法,实现算法动态切换。
(2)出现同一个算法,有很多不同实现的情况下,可以使用策略模式来把这些“不同的实现”实现成为一个算法的类层次。
(3)需要封装算法中,有与算法相关数据的情况下,可以使用策略模式来避免暴露这些跟算法相关的数据结构
(4)出现抽象一个定义了很多行为的类,并且是通过多个 it-else语句来选择这些行为的情况下,可以使用策略模式来代替这些条件语句。
优点:符合开闭原则;避免使用多重条件转移语句;提高算法的保密性和安全性。 缺点:客户端必须知道所有的策略类,并自行决定使用哪一个策略类;产生多个策略类。
2 代码示例
使用场景:假设慕课网的视频课程在不同的时间段会有不同的打折活动。那么可以认为这些不同的打折活动便是不同的策略。
打折策略接口:PromotionStrategy
public interface PromotionStrategy {
void doPromotion();
}
满减策略:ManJianPromotionStrategy
public class ManJianPromotionStrategy implements PromotionStrategy{
public void doPromotion() {
System.out.println("满减促销,满200-20元");
}
}
立减策略:LiJianPromotionStrategy
public class LiJianPromotionStrategy implements PromotionStrategy {
public void doPromotion() {
System.out.println("立减促销,课程的价格直接减去配置的价格");
}
}
返现策略:FanXianPromotionStrategy
public class FanXianPromotionStrategy implements PromotionStrategy{
public void doPromotion() {
System.out.println("返现促销,返回的金额存放到慕课网用户的余额中");
}
}
打折活动:PromotionActivity
public class PromotionActivity {
private PromotionStrategy promotionStrategy;
//构造器注入
public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public void executePromotionStrategy(){
promotionStrategy.doPromotion();
}
}
应用层:Test
public class Test {
public static void main(String[] args) {
PromotionActivity promotionActivity618 = new PromotionActivity(new LiJianPromotionStrategy());
PromotionActivity promotionActivity1111 = new PromotionActivity(new FanXianPromotionStrategy());
//不同阶段采用不同的策略
promotionActivity618.executePromotionStrategy();
promotionActivity1111.executePromotionStrategy();
}
}
在上面应用层时预先定义好了,但如果没有预先定义好,那么可能就会有如下的情况:
public static void main(String[] args) {
PromotionActivity promotionActivity = null;
String promotionKey = "LIJIAN";
if ("LIJIAN".equals(promotionKey)){
System.out.println(123);
promotionActivity = new PromotionActivity(new LiJianPromotionStrategy());
}else if ("MANJAN".equals(promotionKey)){
System.out.println("其他策略");
}
//...其他策略
promotionActivity.executePromotionStrategy();
}
通过不断的进行if else 判断来调整不同的策略,但这种情况肯定不是我们想要的。因此可以采用工厂模式来消除if else
策略工厂:PromotionStrategyFactory
public class PromotionStrategyFactory {
//定义一个map集合来保存不同策略
private static Map<String,PromotionStrategy> Promotion_Strategy_Map = new HashMap<String, PromotionStrategy>();
static {
Promotion_Strategy_Map.put(PromotionKey.LIJIAN,new LiJianPromotionStrategy());
Promotion_Strategy_Map.put(PromotionKey.MANJIAN,new ManJianPromotionStrategy());
Promotion_Strategy_Map.put(PromotionKey.FANXIAN,new FanXianPromotionStrategy());
}
//构造器私有
private PromotionStrategyFactory(){
}
//定义一个无促销的策略
private static PromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();
public static PromotionStrategy getPromotionStrateg(String promotionKey){
PromotionStrategy promotionStrategy = Promotion_Strategy_Map.get(promotionKey);
//当为空时直接返回无促销策略
return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
}
//在声明常量的时候起到了一个分组的作用,默认即为final的不可更改
private interface PromotionKey{
String LIJIAN = "LIJIAN";
String FANXIAN = "FANXIAN";
String MANJIAN = "MANJIAN";
}
}
无促销类:EmptyPromotionStrategy 这个类只是为了对应为空时的情况
public class EmptyPromotionStrategy implements PromotionStrategy{
public void doPromotion() {
System.out.println("无促销");
}
}
应用层:Test
public static void main(String[] args) {
//外界传过来的
String promotionKey = "LIJIAN";
//采用工厂进行实例化
PromotionActivity promotionActivity = new PromotionActivity(PromotionStrategyFactory.getPromotionStrateg(promotionKey));
promotionActivity.executePromotionStrategy();
}
3
4 相关模式
(1)策略模式和状态模式
这两个模式从模式结构上看是一样的,但是实现的功能却是不一样的。状态模式是根据状态的变化来选择相应的行为,不同的状态对应不同的类,每个状态对应的类实现了该状态对应的功能,在实现功能的同时,还会维护状态数据的变化。这些实现状态对应的功能的类之间是不能相互替换的。策略模式是根据需要或者是客户端的要求来选择相应的实现类,各个实现类是平等的,是可以相互替换的。另外策略模式可以让客户端来选择需要使用的策略算法:而状态模式一般是由上下文,或者是在状态实现类里面来维护具体的状态数据,通常不由客户端来指定状态。
(2)策略模式和模板方法模式
这两个模式可组合使用,如同前面示例的那样。模板方法重在封装算法骨架;而策略模式重在分离并封装算法实现。
(3)策略模式和享元模式
这两个模式可组合使用。策略模式分离并封装出一系列的策略算法对象,这些对象的功能通常都比较单一,很多时候就是为了实现某个算法的功能而存在。因此,针对这一系列的、多个细粒度的对象,可以应用享元模式来节省资源,但前提是这些算法对象要被频繁地使用,如果偶尔用一次,就没有必要做成享元了。
0
策略模式(Strategy)---行为型的更多相关文章
- 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成 ...
- 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...
- 策略模式 Strategy 政策Policy 行为型 设计模式(二十五)
策略模式 Strategy 与策略相关的常见词汇有:营销策略.折扣策略.教学策略.记忆策略.学习策略.... “策略”意味着分情况讨论,而不是一概而论 面对不同年龄段的人,面对不同的商品,必然将会 ...
- [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型)
[.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模 ...
- 设计模式22:Strategy 策略模式(行为型模式)
Strategy 策略模式(行为型模式) 动机(Motivation) 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂:而且有时候支持 ...
- [.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型)
[.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它 ...
- 设计模式 笔记 策略模式 Strategy
//---------------------------15/04/28---------------------------- //Strategy 策略模式----对象行为型模式 /* 1:意图 ...
- JAVA设计模式之策略模式 - Strategy
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...
- 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)
在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了“工厂模式”.“策略模式”.“状态模式”等.当然在重构时,有的地 ...
- 设计模式 - 策略模式(Strategy Pattern) 具体解释
策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全 ...
随机推荐
- Hadoop介绍与安装
前言 最近想学习下大数据,有点急于求成,于是去网上找了各种培训机构的视频,发现大都质量不佳,理论基本不说或者简单讲下,然后教你照猫画虎的敲代码,出了问题都没法分析.最后还是找了厦门大学的公开课从理论开 ...
- (八)动态 sql
目录 什么是动态 sql sql 片段 foreach 标签 什么是动态 sql 我们之前在映射文件中,配置 sql 的时候,其实都是静态的 : <!--复杂查询--> <selec ...
- DVWA漏洞演练平台 - 文件上传
DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法的环境,帮助w ...
- php 处理数字为金钱格式
number_format(需要转换的数字,保留小数个数,小数点符号,每三位的分隔符) echo number_format("1000000")."<br> ...
- Servlet获取JSP中的汉字乱码问题解决方案
1.String customerName=request.getParameter("customer_name");这样会出现乱码 解决方案很简单: String custom ...
- WebApi 空项目生成帮助文档
1.创建一个WebApi的空项目 2.写一些接口,在接口文档中忽略某个方法可以使用 [ApiExplorerSettings(IgnoreApi = true)] 特性 3.在Nuget中添加 Mi ...
- 【Android】笔记
一.环境搭建 1. 下载JDK, JRE , 设置JDK 和 JRE环境变量 , PATH C:\jdk1.6.0_15\bin; JAVA_HOME C:\jdk1.6.0_15 重启电脑, ...
- react hooks学习
接触React项目快两个月了,还在研究摸索各种知识点的过程中,充实且幸福. 在项目中学习新知识,还是很有效率的,一边写项目,一边实验新的知识点,比如react hooks!嘻嘻嘻~~~ 写了好一段时间 ...
- 数据分析基础之pandas & numpy
一.jupyter的常用快捷键 - 插入cell: a, b a是after从后插入 a是before 从前插入 - 删除cell: dd, x 都可以 - 修改cell的模式:m, y - t ...
- 使用2种python脚本工具将2个txt文档中的文字进行比较,并计算出Corr, WER正确率,准确率
一.准备: linux服务器,src2mlf.py rec2mlf.py HResults文件,1份源文件和1份需要对比的文件.文件放置于本人云盘 二.使用方法: 1. 对比工具 HResul ...