zoukankan      html  css  js  c++  java
  • 设计模式:策略模式

    什么是策略模式

      策略是我们在处理问题是所采用的步骤方法。如我要过年回家,方式有飞机、火车,汽车等,这几种方式就是策略。

    再比如我们商城要搞一个活动,给用户生成一批优惠券,5折,7折,免单等,这些方法也是策略。

    一个策略模式的例子:

      模拟一个销售给客户报价的业务场景。我们销售人员对不同的客户制定不同的报价。

        普通客户----不打折

        一星客户----9折

        二星客户----8折

        三星客户--- 7折

      如果不采用策略模式大概处理方式如下:

    	public double getPrice(String type, double price){
    		if(type.equals("普通客户")){
    			System.out.println("不打折,原价");
    			return price;
    		}else if(type.equals(“一星客户")){
    			return price*0.9;
    		}else if(type.equals(“二星客户")){
    			return price*0.8;
    		}else if(type.equals(“三星客户")){
    			return price*0.7;
    		}
    		return price;
    	}
    

      简单粗暴,非常容易。但一但类型较多,业务处理复杂,流程长时,或者要增加业务时,整个代码控制会难以维护。

    采用策略模式能将这类问题很好处理,将每个分支问题都生成一个业务子类,具体用哪个子类就根据业务选择。

    采用策略模式:

    /**
     * 抽象策略对象
     */
    public interface Stratgey {
    
        double getPrice(double price);
    }
    

      

    /**
     * 普通客户 不打折
     */
    public class NewCustomerStrategy implements Stratgey {
        @Override
        public double getPrice(double price) {
            System.out.println("普通客户不打折");
            return price;
        }
    }
    

      

    /**
     * 一级客户 9折
     */
    public class LeaveOneCustomerStrategy implements Stratgey {
        @Override
        public double getPrice(double price) {
            System.out.println("一级客户9折");
            return price;
        }
    }
    

      

    /**
     * 二级客户8折
     */
    public class LeaveTwoCustomerStrategy implements Stratgey {
        @Override
        public double getPrice(double price) {
            System.out.println("二级客户8折");
            return price;
        }
    }
    

      

    /**
     * 三级客户7折
     */
    public class LeaveThreeCustomerStrategy implements Stratgey {
        @Override
        public double getPrice(double price) {
            System.out.println("三级客户7折");
            return price;
        }
    }
    

      再来一个管理策略对象的类

    /**
     * 负责和具体策略类交互
     *实现算法和客户端分离
     */
    public class Context {
    
        //当前的策略对象
        private Stratgey stratge;
    
        public Context(Stratgey stratge) {
            this.stratge = stratge;
        }
    
        public void printPrice(double s){
            stratge.getPrice(s);
        }
    }
    

      

    public class Client {

    public static void main(String[] args) {
    //不同的策略
    Stratgey stratge = new NewCustomerStrategy();
    Stratgey one = new LeaveOneCustomerStrategy();
    Stratgey two = new LeaveTwoCustomerStrategy();
    Stratgey three = new LeaveThreeCustomerStrategy();

    //注入具体的策略
    Context context1 = new Context(stratge);
    context1.printPrice(998);
    Context context2 = new Context(one);
    context2.printPrice(998);
    Context context3 = new Context(two);
    context3.printPrice(998);
    }
    }

      当对应不同的客户时,就注入不同的销售策略。

    策略模式将不同的算法封装成一个对象,这些不同的算法从一个抽象类或者一个接口中派生出来,客户端持有一个抽象的策略的引用,这样客户端就能动态的切换不同的策略。

    和工厂模式区别在于策略模式自己实现业务处理过程,工厂模式在于所有业务交由工厂方法处理。

  • 相关阅读:
    数据结构_bubble_sort
    数据结构_sfdg(小F打怪)
    数据结构_yjjsj(伊姐姐数字游戏)
    数据结构 queue
    数据结构 Job
    数据结构 happiness
    数据结构_calculator
    js并归排序的思路
    js神秘的电报密码---哈弗曼编码
    js同时获得数组的两个最小值
  • 原文地址:https://www.cnblogs.com/gcm688/p/10289804.html
Copyright © 2011-2022 走看看