一、需求

  根据用户vip等级来返回不同的价格,vip等级是不固定的,随时可能要增加,价格也不是固定的。

二、常规的写法

/**
* 如果有新增类型,就需要频繁的修改此处的代码!
* 不符合开闭原则!
* @author DUCHONG
*
*/
public class CommonGetPrice { public static final String VIP1="v1";
public static final String VIP2="v2";
public static final String VIP3="v3";
public static final String VIP4="v4"; /**
* 获取vip的价格
* @param type vip类型
* @param price 原价格
* @return
*/
public static double getVipPrice(String type, double price) { if (type.equals(VIP1)) {
System.out.println("不使用策略模式---不打折,原价");
return price;
}
else if (type.equals(VIP2)) {
System.out.println("不使用策略模式---打九折");
return price * 0.9;
}
else if (type.equals(VIP3)) {
System.out.println("不使用策略模式---打八五折");
return price * 0.85;
}
else if (type.equals(VIP4)) {
System.out.println("不使用策略模式---打八折");
return price * 0.8;
}
return price;
} }

三、使用策略模式

3.1、定义策略接口

/**
* 策略接口
* @author DUCHONG
*/
public interface Strategy { double getVipPrice(double originPrice);
}

3.2、定义上下文

  上下文持有策略接口的引用,将算法与调用者隔离。

/**
* 负责和具体的策略类交互
* 这样的话,具体的算法和直接的客户端调用分离了,使得算法可以独立于客户端独立的变化。
* 如果使用spring的依赖注入功能,还可以通过配置文件,动态的注入不同策略对象,动态的切换不同的算法.
* @author DUCHONG
*
*/
public class VipContext { /**
* 当前采用的算法对象
*/
private Strategy strategy; public VipContext() { }
/**
* 可以通过构造器来注入
*/
public VipContext(Strategy strategy) {
super();
this.strategy = strategy;
}
/**
* 可以通过set方法来注入
*/
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
} public double getVipPrice(double originPrice){
return strategy.getVipPrice(originPrice);
} }

3.3、不同算法实现类

/**
* @author DUCHONG
* @since 2019-09-24 18:28
**/
public class VipOneStrategy implements Strategy { /**
* vip1 不打折
* @param originPrice
* @return
*/
@Override
public double getVipPrice(double originPrice) { System.out.println("使用策略模式---不打折,原价");
return originPrice;
}
} /**
* @author DUCHONG
* @since 2019-09-24 18:28
**/
public class VipTwoStrategy implements Strategy{ @Override
public double getVipPrice(double originPrice) {
System.out.println("使用策略模式---打九折");
return originPrice * 0.9;
}
} /**
* @author DUCHONG
* @since 2019-09-24 18:28
**/
public class VipThreeStrategy implements Strategy{ @Override
public double getVipPrice(double originPrice) {
System.out.println("使用策略模式---打八五折");
return originPrice * 0.85;
}
} /**
* @author DUCHONG
* @since 2019-09-24 18:28
**/
public class VipFourStrategy implements Strategy{ @Override
public double getVipPrice(double originPrice) {
System.out.println("使用策略模式---打八折");
return originPrice * 0.8;
}
}

四、Client

public class Client {

    public static void main(String[] args) {

        double originPrice=1000D;

        //使用策略模式之前
System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP1,originPrice));
System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP2,originPrice));
System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP3,originPrice));
System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP4,originPrice)); //使用策略模式之后
VipContext vipContext=new VipContext(); Strategy v1=new VipOneStrategy();
vipContext.setStrategy(v1);
System.out.println(vipContext.getVipPrice(originPrice)); Strategy v2=new VipTwoStrategy();
vipContext.setStrategy(v2);
System.out.println(vipContext.getVipPrice(originPrice)); Strategy v3=new VipThreeStrategy();
vipContext.setStrategy(v3);
System.out.println(vipContext.getVipPrice(originPrice)); Strategy v4=new VipFourStrategy();
vipContext.setStrategy(v4);
System.out.println(vipContext.getVipPrice(originPrice)); }
}

五、结果

最简单的策略模式代替if-else实战的更多相关文章

  1. 简单的策略模式Strategy演示

    策略模式,即规则在变化之中,结果终归为一. 公司给员工计算工资,如有加班费,差旅费,每个月的生活补帖等等其它费用需要计算.这个费的规则是不尽相同. 不管策略的规则怎样,终归需要计算出一个结果 工资: ...

  2. Android使用的设计模式2——策略模式

    今天讲解一下策略模式,策略模式也是很常用的设计模式,对多种算法或者数据结构选择使用的情况下,经常会使用策略模式来管理这些算法.下面会简单讲解一下策略模式的概念和基本实现.然后结合Android里面的实 ...

  3. Android设计模式之策略模式

    今天介绍下策略模式,直接先上UML图 策略模式的概念 The Strategy Pattern defines a family of algorithms,encapsulates each one ...

  4. Android设计模式—策略模式

    1.策略模式概念 定义一系列算法,把他们独立封装起来,并且这些算法之间可以相互替换.策略模式主要是管理一堆有共性的算法,客户端可以根据需要,很快切换这些算法,并且保持可扩展性. 策略模式的本质:分离算 ...

  5. JavaScript中的设计模式:策略模式

    无论学习前端还是后端设计模式是作为一名程序员不可缺少的知识,就像下底传中对于一个边锋来说. 一.策略模式 策略模式给人的第一感觉就是在代码里面消除了很多if-else分支语句,比如一个求员工奖金的程序 ...

  6. Head First 设计模式 第1章 策略模式

    本章从浅入深的讲解了策略模式的使用,以及策略模式中所涉及到的几个设计原则,在本章的最后给出了策略模式的定义. 1.定义及优点 什么是策略模式呢? 答:定义算法族(对象),分别封装起来,让他们之间可以相 ...

  7. 策略模式(Strategy )

    为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy {    public void algorithmInterface(); ...

  8. swift设计模式学习 - 策略模式

    移动端访问不佳,请访问我的个人博客 设计模式学习的demo地址,欢迎大家学习交流 策略模式 策略模式定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户. ...

  9. Strategy 策略模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. DBA 有哪些工作

    首先,我们看看DBA的工作有哪些?DBA的工作实际上都是围绕数据库展开,包含但不限于这些工作: 1. 数据库.主机.操作系统.交换机.存储选型,预算,架构设计,部署,参数优化: 2. 数据库备份.恢复 ...

  2. H3CNE学习5 STP

    一.STP 1.概念 2.STP开机默认会运行 二.STP操作 1.原理 2.根桥选举,首先比前面的ID,谁小谁就是根桥,如果ID一样就比较mac,谁小谁就是根桥 可以手动修改优先级,图中可以将swA ...

  3. Kibana 的安装(Windows版本)

    一.安装 安装条件 保证安装了JDK 保证安装node 保证安装了Elasticsearch 1.点击kibana官网下载  [kibana的版本和elasticsearch的版本和必须一致] 2.修 ...

  4. 洛谷 P1006 传纸条 题解

    P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法 ...

  5. Android精通教程-Android入门简介

    前言 大家好,我是 Vic,今天给大家带来Android精通教程-Android入门简介的概述,希望你们喜欢 每日一句 If life were predictable it would cease ...

  6. PHP连接MySQL创建表

    源代码: <?php header("Content-Type:text/html;charset=utf8");//声明编码格式 $conn=mysqli_connect( ...

  7. 微信小程序 长按文字复制与按钮复制

    1. 长按文字复制 当要实现长按文字进行复制的时候,需要使用text标签,并将selectable属性设置为true <text class='url-txt' selectable='true ...

  8. locust使用命令

    locust -f locust_demo.py --logfile=locusfile.log

  9. C# WinForm中的一些小问题

    前言 记一些C#窗体应用编程中的小问题. DataGridView 设置Selected = true后实际选中行标依旧是第一行. 通常通过this.dataGridView1.Rows[i].Sel ...

  10. 4 个用于执行高级数学计算的 JavaScript 库

    在使用JavaScript执行数学方面的任务时,往往要用到浮点运算,且需要精确到某位小数,这就容易造成错误,而且会相当费时.因此,如果你需要做一些高精度的数学计算的编程工作,比如财务或科学计算,那么你 ...