zoukankan      html  css  js  c++  java
  • 【GOF23设计模式】策略模式

    来源:http://www.bjsxt.com/ 
    一、【GOF23设计模式】_策略模式、CRM中报价策略、GUI编程中布局管理器底层架构

    场景

    我们先

     1 package com.test.strategy;
     2 /**
     3  * 实现起来比较容易,符合一般开发人员的思路
     4  * 假如,类型特别多,算法比较复杂时,整个条件语句的代码就变得很长,难于维护。
     5  * 如果有新增类型,就需要频繁地修改此处的代码!
     6  * 不符合开闭原则!
     7  */
     8 public class TestStrategy {
     9     public double getPrice(String type,double price){
    10         if(type.equals("普通客户小批量")){
    11             System.out.println("不打折,原价");
    12             return price;
    13         }
    14         if(type.equals("普通客户大批量")){
    15             System.out.println("打九折");
    16             return price*0.9;
    17         }
    18         if(type.equals("老客户小批量")){
    19             System.out.println("打八五折");
    20             return price*0.85;
    21         }
    22         if(type.equals("老客户大批量")){
    23             System.out.println("打八折");
    24             return price*0.8;
    25         }
    26         return price;
    27     }
    28 }
    策略模式
    1 package com.test.strategy;
    2 
    3 public interface Strategy {
    4     public double getPrice(double standardPrice);
    5 }
     1 package com.test.strategy;
     2 
     3 public class NewCustomerFewStrategy implements Strategy{
     4 
     5     @Override
     6     public double getPrice(double standardPrice) {
     7         System.out.println("不打折,原价");
     8         return standardPrice;
     9     }
    10 }
     1 package com.test.strategy;
     2 
     3 public class NewCustomerManyStrategy implements Strategy{
     4 
     5     @Override
     6     public double getPrice(double standardPrice) {
     7         System.out.println("打九折");
     8         return standardPrice*0.9;
     9     }
    10 }
     1 package com.test.strategy;
     2 
     3 public class OldCustomerFewStrategy implements Strategy{
     4 
     5     @Override
     6     public double getPrice(double standardPrice) {
     7         System.out.println("打八五折");
     8         return standardPrice*0.85;
     9     }
    10 }
     1 package com.test.strategy;
     2 
     3 public class OldCustomerManyStrategy implements Strategy{
     4 
     5     @Override
     6     public double getPrice(double standardPrice) {
     7         System.out.println("打八折");
     8         return standardPrice*0.8;
     9     }
    10 }
     1 package com.test.strategy;
     2 /**
     3  * 负责和具体的策略类交互
     4  * 这样的话,具体的算法和直接的客户端调用分离了,使得算法可以独立于客户端独立地变化。
     5  * 可以通过构造器来注入算法对象
     6  * 也可以通过set方法来注入
     7  * 如果使用spring的依赖注入功能,还可以通过配置文件,动态地注入不同策略对象,动态地切换不同的算法。
     8  */
     9 public class Context {
    10     private Strategy strategy;//当前采用的算法对象
    11 
    12     //可以通过构造器来注入
    13     public Context(Strategy strategy) {
    14         super();
    15         this.strategy = strategy;
    16     }
    17     //也可以通过set方法来注入
    18     public void setStrategy(Strategy strategy) {
    19         this.strategy = strategy;
    20     }
    21 
    22     public void printPrice(double s){
    23         System.out.println("你该报价:"+strategy.getPrice(s));
    24     }
    25 }
     1 package com.test.strategy;
     2 
     3 public class Client {
     4     public static void main(String[] args) {
     5         Strategy s1 = new OldCustomerManyStrategy();
     6         Context ctx = new Context(s1);
     7 
     8         ctx.printPrice(988);
     9     }
    10 }
    控制台输出:
    打八折
    你该报价:790.4000000000001

    本质

  • 相关阅读:
    mysql 8.x 忘记密码
    sqlite
    公租房查询记录
    idea 常用快捷键总结
    vue h5项目中上传图片时保存之后显示不全
    微信小程序开发中使用npm命令快速添加页面
    【Luogu7816】「Stoi2032」以父之名
    【XSY3657】 因数分解
    Min_25筛学习笔记
    拉格朗日反演学习笔记
  • 原文地址:https://www.cnblogs.com/erbing/p/5802643.html
Copyright © 2011-2022 走看看