一模式定义
策略模式:定义一系列的算法,将每一种算法封装起来并可以相互替换使用,策略模式让算法独立于使用它的客户应用而独立变化。
二模式举例
1模式分析
我们借用商场促销商品来说明这一模式。
2策略模式静态类图
3代码示例
3.1 创建策略接口一IStrategy
- package com.demo.strategy;
- /**
- * 策略接口
- *
- * @author
- *
- */
- public interface IStrategy {
- /**
- * 计算实际价格方法
- *
- * @param consumePrice
- * 消费金额
- * @return
- */
- public double realPrice(double consumePrice);
- }
3.2 八折促销策略一RebateStrategy
- package com.demo.strategy;
- /**
- * 打八折商品促销策略
- *
- * @author
- *
- */
- public class RebateStrategy implements IStrategy {
- private final double rate;
- /**
- * 构造方法设置打折率
- */
- public RebateStrategy() {
- this.rate = 0.8;
- }
- /**
- * 计算实际价格方法
- *
- * @param consumePrice
- * 消费金额
- * @return
- */
- public double realPrice(double consumePrice) {
- return consumePrice * this.rate;
- }
- }
3.3 满1000减200促销策略一ReduceStrategy
- package com.demo.strategy;
- /**
- * 满1000减200 商品促销策略
- *
- * @author
- *
- */
- public class ReduceStrategy implements IStrategy {
- /**
- * 计算实际价格方法
- *
- * @param consumePrice
- * 消费金额
- * @return
- */
- public double realPrice(double consumePrice) {
- if (consumePrice >= 1000) {
- return consumePrice - 200;
- } else {
- return consumePrice;
- }
- }
- }
3.4 200以上部分打8折促销策略一PromotionalStrategy
- package com.demo.strategy;
- /**
- * 满200,高于200部分打八折 商品促销策略
- *
- * @author
- *
- */
- public class PromotionalStrategy implements IStrategy {
- /**
- * 计算实际价格方法
- *
- * @param consumePrice
- * 消费金额
- * @return
- */
- public double realPrice(double consumePrice) {
- if (consumePrice > 200) {
- return 200 + (consumePrice - 200) * 0.8;
- } else {
- return consumePrice;
- }
- }
- }
3.5 创建上下文环境一Context
- package com.demo.context;
- import java.math.BigDecimal;
- import com.demo.strategy.IStrategy;
- /**
- * 上下文环境
- *
- * @author
- *
- */
- public class Context {
- // 当前策略
- private IStrategy strategy;
- // 设置当前策略
- public void setStrategy(IStrategy strategy) {
- this.strategy = strategy;
- }
- // 使用策略计算价格
- public double cul(double consumePrice) {
- // 使用具体商品促销策略获得实际消费金额
- double realPrice = this.strategy.realPrice(consumePrice);
- // 格式化保留小数点后1位,即:精确到角
- BigDecimal bd = new BigDecimal(realPrice);
- bd = bd.setScale(1, BigDecimal.ROUND_DOWN);
- return bd.doubleValue();
- }
- }
3.6 消费者购物消费一Client
- package com.demo;
- import java.util.Random;
- /**
- * 客户端应用程序
- *
- * @author
- *
- */
- public class Client {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 创建上下问环境对象实例
- // Context context = new Context();
- // 随机数对象
- Random random = new Random();
- for (int i = 0; i < 10; i++) {
- // 产生随机数的方式判断使用何种促销策略
- int x = random.nextInt(3);
- // 消费价格也是由随机数产生的(不能为0)
- double consumePrice = 0;
- while ((consumePrice = random.nextInt(2000)) == 0) {
- }
- double realPrice = consumePrice;
- switch (x) {
- case 0:
- // 打八折商品
- // context.setStrategy(new RebateStrategy());
- realPrice = consumePrice * 0.8;
- break;
- case 1:
- // 满200,高于200部分打八折 商品
- // context.setStrategy(new PromotionalStrategy());
- if (consumePrice > 200) {
- realPrice = 200 + (consumePrice - 200) * 0.8;
- }
- break;
- case 2:
- // 满1000减200 商品
- // context.setStrategy(new ReduceStrategy());
- if (consumePrice >= 1000) {
- realPrice = consumePrice - 200;
- }
- break;
- }
- System.out.print("【"
- + (x == 0 ? "打八折" : (x == 1 ? "高于200部分打八折"
- : (x == 2 ? "满1000减200" : ""))) + "】商品:");
- System.out.println("原价:" + consumePrice + " - 优惠后价格:" + realPrice);
- }
- }
- }
4运行结果
【满1000减200】商品:原价:908.0 - 优惠后价格:908.0
【满1000减200】商品:原价:1129.0 - 优惠后价格:929.0
【满1000减200】商品:原价:829.0 - 优惠后价格:829.0
【打八折】商品:原价:518.0 - 优惠后价格:414.40000000000003
【满1000减200】商品:原价:1230.0 - 优惠后价格:1030.0
【打八折】商品:原价:106.0 - 优惠后价格:84.80000000000001
【满1000减200】商品:原价:1134.0 - 优惠后价格:934.0
【高于200部分打八折】商品:原价:664.0 - 优惠后价格:571.2
【满1000减200】商品:原价:564.0 - 优惠后价格:564.0
【满1000减200】商品:原价:730.0 - 优惠后价格:730.0
三该模式设计原则
1"开-闭"原则
2单一职责原则
四使用场合
1当多个类的表现行为不同,需要在运行时刻动态选择具体执行的行为的时候。
2需要在不同情况下使用不同策略,或者策略还可能在未来用其它方式实现的时候。
3需要隐藏具体策略的实现细节,各个具体策略彼此独立的时候。
4当一个类中出现了多种行为,而且在一个操作中使用多个条件分支来判断使用多种行为的时候,可以使用策略模式将各个条件分支的动作植入具体策略中实现。
五策略模式静态类图
相关推荐
策略模式结合模板方法模式
策略模式的 C++ 代码实现, ide :XCode
策略模式定义了方法族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。 还可以那家咖啡屋,记忆里很温馨,很sugar,或许很多温馨美好的回忆都是于咖啡有关的。 我们常常说设计...
所有模式都可分为类模式和对象模式两种,类模式是继承,对象模式是委托,而桥接模式和策略模式都是将任务委托给另外一个接口去实现,那么两者的区别什么呢?
主要介绍了详解SpringBoot结合策略模式实战套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
技术: 1. spring事件驱动(ApplicationEventPublisher) 2. 策略模式处理事件 目的: 1. 通过event,代码逻辑异步处理 2. 通过策略模式,构建具体监听实现 3. 解耦 4. 容错(降低代码块错误风险)
这是策略模式中的一个经典实例,通过鸭子问题,能让学习者更好的了解设计模式,这也是headfirst 设计模式中用的经典实例
深入浅出设计模式之抽象工厂模式+工厂方法模式+策略模式实现手机加工厂(加类图)
策略模式实例策略模式实例策略模式实例策略模式实例策略模式实例
策略模式代码,两种方式实现,第一种,通过map存储方式,第二种,通过配置文件加反射方式
策略模式的简单例子,根据《Head First设计模式》中第一章中的Duck编写
策略模式例子,纯代码,copy后即可运行; 1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。 2、一个系统需要动态地在几种算法中...
策略模式在实际项目中的应用二,该测试demo为普通项目,导入build path一下lib目录下的jar包,然后运行测试类即可在控制台看到测试结果
处理层的设计采用了设计模式中的策略模式、模板方法模式和工厂模式。 Server端和Client端的实现也采用了分层的设计方式,包含自定义的模型层、视图层和控制层。 说明:程序采用配置文件的方式进行初始化,运行时时请...
55-Java设计模式之策略模式与状态模式1
设计模式中的策略模式,同时兼容简单工厂模式,商场收银模式,易懂可用。
策略模式策略模式策略模式
javascript的策略模式和代理模式简介,附代码
在网络上看到几个加密解密算法,本着"取之于民用之于民"的方针测试一下,并用策略模式将这些算法封装共享。
设计模式中的策略模式,template模式详细讲解。