1    代码演练

1.1  代码演练1(多态)

1.2  代码演练2(优化应用)

1.3  代码演练3(策略模式结合工厂模式)

1    代码演练

1.1  代码演练1(多态)

需求:

木木网卖课程,为了促进销售,618实行买课程立减10元,双十一实行满50减10元,还有返现的优惠(这个活动还没有开始)。请实现它

uml类图:

测试类:

package com.geely.design.pattern.behavioral.strategy;

public class TestStrategy {
public static void main(String [] args){
PromotionActivity promotionActivityLJ = new PromotionActivity(new LiJianPromotionStrategy());
PromotionActivity promotionActivityMJ = new PromotionActivity(new
ManJianPromotionStrategy()); promotionActivityLJ.execute();
promotionActivityMJ.execute();
}
}

应用层:

package com.geely.design.pattern.behavioral.strategy;

public class PromotionActivity {
private PromotionStrategy promotionStrategy;
public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public void execute(){
promotionStrategy.doPromotion();
}
}

实现类1:

package com.geely.design.pattern.behavioral.strategy;

public class LiJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("立减10元");
}
}

实现类2:

package com.geely.design.pattern.behavioral.strategy;

public class ManJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("满50元减10元");
}
}

实现类3:

package com.geely.design.pattern.behavioral.strategy;

public class FanXianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("返现10元到木木网");
}
}

接口:

package com.geely.design.pattern.behavioral.strategy;

/**
* 初始接口
*/
public interface PromotionStrategy {
void doPromotion();
}

打印日志:

立减10元
满50元减10元 Process finished with exit code 0

1.2  代码演练2(优化应用类)

测试类优化:

package com.geely.design.pattern.behavioral.strategy;

import org.apache.commons.lang.StringUtils;

public class TestStrategy {

    public static void main(String [] args){
PromotionActivity promotionActivity = null;
String promotionKey = "ManJian";
if(StringUtils.equals(promotionKey,"LiJian")){
promotionActivity = new PromotionActivity(new LiJianPromotionStrategy());
}else if(StringUtils.equals(promotionKey,"ManJian")){
promotionActivity = new PromotionActivity(new ManJianPromotionStrategy());
}
promotionActivity.execute();
}
}

1.3  代码演练3(策略模式结合工厂模式)

目的:

a  将参数设置成可以配置的,简化以后维护的成本

b  取消大量的if...else...应用

测试类:

package com.geely.design.pattern.behavioral.strategy;

import org.apache.commons.lang.StringUtils;

/**
* add by ddwei 20200213
*/
public class TestStrategy {
//demo 1 常规多态实现
/* public static void main(String [] args){
PromotionActivity promotionActivity618 = new PromotionActivity(new FanXianPromotionStrategy());
PromotionActivity promotionActivity1111 = new PromotionActivity(new LiJianPromotionStrategy());
promotionActivity1111.executePromotion();
promotionActivity618.executePromotion();
}*/ //demo 2 正常程序不可避免 的使用if else
/*public static void main(String [] args){
String promotionStrategy = "LIJIAN";
PromotionActivity promotionActivity = null;
if(StringUtils.equals(promotionStrategy,"LIJIAN")){
promotionActivity = new PromotionActivity(new LiJianPromotionStrategy());
}else if(StringUtils.equals(promotionStrategy,"FANXIAN")){
promotionActivity = new PromotionActivity(new FanXianPromotionStrategy());
} //...
promotionActivity.executePromotion();
}*/ //demo 3 结合工厂模式 ,策略模式推进 ,不再使用if else
public static void main(String [] args){
//String promotionStrategy = "LIJIAN";
//String promotionStrategy = "FANXIAN";
String promotionStrategy = "LIJIAN3";
PromotionStrategy iPromotionStrategy = PromotionFactory.getPromotionStrategy(promotionStrategy);
PromotionActivity promotionActivity = new PromotionActivity(iPromotionStrategy);
promotionActivity.executePromotion();
} }

核心类(策略模式结合工厂模式):

package com.geely.design.pattern.behavioral.strategy;

import java.util.HashMap;
import java.util.Map; /**
* 策略工厂
*/
public class PromotionFactory { private static Map<String,PromotionStrategy> promotionStrategyHashMap= new HashMap<String, PromotionStrategy>();
//静态代码块 HashMap放值
static{
promotionStrategyHashMap.put(IPromotionConfigtation.MANJIAN,new ManJianPromotionStrategy());
promotionStrategyHashMap.put(IPromotionConfigtation.LIJIAN,new LiJianPromotionStrategy());
promotionStrategyHashMap.put(IPromotionConfigtation.FANXIAN,new FanXianPromotionStrategy());
} /**
* 主方法
* 根据传入的策略key值 查询到相应的策略.
* 如果未查到相应的key值返回空策略,目的是避免出现空指针异常的情况
* 从根本上代替了if...else 的功能
* @param promotionKey
* @return
*/
public static PromotionStrategy getPromotionStrategy(String promotionKey){
PromotionStrategy EMPTYPROMOTIONSTRATEGY = new EmptyPromotionStrategy();
return promotionStrategyHashMap.containsKey(promotionKey)?promotionStrategyHashMap.get(promotionKey):EMPTYPROMOTIONSTRATEGY;
} /**
* 后期维护入口:
* 内部类,放入配置参数
*/
interface IPromotionConfigtation{
String MANJIAN = "MANJIAN";
String LIJIAN = "LIJIAN";
String FANXIAN = "FANXIAN";
} //禁止外部调用构造函数来创建对象
public PromotionFactory() {
}
}

促销活动类:

package com.geely.design.pattern.behavioral.strategy;

/**
* 促销应用类
* add by ddwei 20200213
*/
public class PromotionActivity {
//有参构造传值
private PromotionStrategy promotionStrategy; public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
} //调用执行策略方法
public void executePromotion(){
promotionStrategy.doPromotion();
} }

子类空策略类:

package com.geely.design.pattern.behavioral.strategy;

/**
* 空策略
*/
public class EmptyPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("todo 无相应的优惠!");
}
}

子类满减类:

package com.geely.design.pattern.behavioral.strategy;

public class ManJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("todo 实现满减逻辑");
}
}

子类立减类:

package com.geely.design.pattern.behavioral.strategy;

public class LiJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("todo 实现立减逻辑");
}
}

子类返现类:

package com.geely.design.pattern.behavioral.strategy;

public class FanXianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("todo 实现返现逻辑");
}
}

父类策略接口:

package com.geely.design.pattern.behavioral.strategy;

/**
* 促销通用接口
* add by ddwei 20200213
*/
public interface PromotionStrategy {
void doPromotion();
}

打印结果:

todo 无相应的优惠!

Process finished with exit code 0

设计模式课程 设计模式精讲 19-2 策略模式coding的更多相关文章

  1. 设计模式课程 设计模式精讲 13-2 享元模式coding

    1 代码演练 1.1 代码演练1 1 代码演练 1.1 代码演练1 需求: 每周由随机部门经历做报告: 重点关注: a 该案例是单例模式和享元模式共同使用 b 外部传入的department是外部状态 ...

  2. 设计模式课程 设计模式精讲 11-2 装饰者模式coding

    1 代码演练 1.1 代码演练1(未使用装饰者模式) 1.2 代码演练2(使用装饰者模式) 1 代码演练 1.1 代码演练1(未使用装饰者模式) 需求: 大妈下班卖煎饼,加一个鸡蛋加一元,一个火腿两元 ...

  3. 设计模式课程 设计模式精讲 7-2 建造者模式Coding

    1 代码演练 1.1 建造者模式演练 1.2 静态内部类演练建造者模式(链式调用) 1 代码演练 1.1 建造者模式演练 需求: 根据讲师提供的课程名称,课程ppt,课程视频,课程手记,课程问答 制作 ...

  4. 设计模式课程 设计模式精讲 17-2 模板方法模式coding

    1 代码演练 1.1 代码演练1 1.2 代码演练2(后端课程子类运用钩子方法,加入写手记的方法) 1.3 代码演练3(前端有多个子类,有得需要写手记,有得不需要写,如何实现?) 1 代码演练 1.1 ...

  5. 《Head First 设计模式》读书笔记(1) - 策略模式

    <Head First 设计模式>(点击查看详情) 1.写在前面的话 之前在列书单的时候,看网友对于设计模式的推荐里说,设计模式的书类别都大同小异,于是自己就选择了Head First系列 ...

  6. PHP 设计模式 笔记与总结(8)策略模式

    ① 策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式就是策略模式. ② 实际应用举例,假如一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示 ...

  7. CSharp设计模式读书笔记(22):策略模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)

    策略模式(Strategy Pattern):定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 模式角色与结构: ...

  8. 设计模式课程 设计模式精讲 6-2 抽象工厂coding

    1 代码讲解 1.1 抽象工厂优点 1.2 抽象工厂缺点 1.3 为何有产品族的业务场景宜用抽象工厂设计模式?而不是工厂设计模式? 2 代码演练 2.1 抽象工厂代码演练 1 代码讲解 1.1 抽象工 ...

  9. 设计模式课程 设计模式精讲 18-2 迭代器模式coding

    1 代码演练 1.1 代码演练1(迭代器模式演练) 1.2 代码使用场景 1 代码演练 1.1 代码演练1(迭代器模式演练) 需求: 课程管理:需要实现课程可进行增添,删除,并能够打印出课程列表. u ...

随机推荐

  1. python中文本的读写操作

    文本的操作 函数的排序操作: def func(i): return i[2] list=[('曹操',101,'c'),('吕布',100,'d'),('刘备',200,'l'),('大乔',50, ...

  2. linux 系统 grep 命令 摘录过滤特定的行

    1.grep ‘xxx’ filename  enter   其中xxx为要搜索的字符串 ,即可检索到含有 xxx的行. 2.grep 'xxx' filename  >newfilename ...

  3. 洛谷 P2239 螺旋矩阵(模拟 && 数学)

    嗯... 题目链接:https://www.luogu.org/problem/P2239 这道题首先不能暴力建图,没有简单方法,只有进行进行找规律. AC代码: #include<cstdio ...

  4. oracle错误代码大全(超详细)

    本篇文章是对oracle错误代码进行了详细的总结与分析,需要的朋友参考下 ORA-00001: 违反唯一约束条件 (.)ORA-00017: 请求会话以设置跟踪事件ORA-00018: 超出最大会话数 ...

  5. 2.2.FastDFS-单机拆分版-存储器安装配置

    Centos610系列配置 我们在Centos610FastDFS单机模式-FastDFS安装 中已经完成了FastDFS的安装,接下来我们进行FastDFS存储器的安装. 1.找到FastDFS配置 ...

  6. 一个HTTP数据包的奇幻之旅

    我是一个HTTP数据包,不知谁创建了我,把我丢到这个房间. 突然,来了一个大汉,我吓得缩到角落. “该启程了,站起来”. “去哪里啊?” 我弱弱的问. “还能去哪里,你是一个数据包,当然要出远门,完成 ...

  7. 【渗透测试】Msf提权步骤

    1.生成反弹木马(脚本,执行程序) msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=&l ...

  8. 源代码管理工具(2)——SVN(2)——第一次用SVN遇到的问题

    今天因为项目的需要第一次使用了svn来托管项目,第一使用svn遇到了几个问题. 这个安装的过程很简单,不再赘述.在安装完成之后,相信肯定有一部分第一次用这个的人直接到开始处打开这个软件,这时候就会弹出 ...

  9. bootstrap与vue,react的区别

    链接(与Vue区别):https://www.php.cn/faq/423095.html 链接(BootStrap, React, Vue的比较):https://www.jianshu.com/p ...

  10. iOS 开发之 23种设计模式

    整理了 iOS 开发中用到的设计模式: iOS 开发之 设计模式[一]原型模式 (Prototype pattern) iOS 开发之 设计模式[二]工厂方法模式 iOS 开发之 设计模式[三]抽象工 ...