美文网首页
设计模式---策略(Stragety)

设计模式---策略(Stragety)

作者: RalapHao | 来源:发表于2017-09-28 16:17 被阅读0次

    介绍

    一系列的算法,并将每一个算法封装起来,而且使他们还可以相互替换,策略模式让算法独立与使用它的客户而独立变化。
    通俗讲:提供一个统一的接口,不同的算法或策略动态的替换,这种模式的可扩展性、可维护性、也就更高,

    应用场景

    1. 针对同一类型的多种处理方式,仅仅是具体行为有差别时。
    2. 需要安全的封装多种同一类型的操作时;
    3. 出现同一抽象类有多个子类,而又需要使用if-else或者switch-case来选择具体的子类时;

    UML

    UML.png

    代码

    1. 首先就一个交通计费,不同的交通工具有不同计费规则,平时我们会这样写
    public class PriceCalculator {
    
        private static final int BUS = 0;
        private static final int SUBWAY = 1;
    
        /**
         * 公交计费
         */
        private int busProice(int km) {
            int extraTotal = km - 10;
            int extraFactor = extraTotal / 5;
            int fraction = extraFactor % 5;
            int price = 1 + extraFactor * 1;
            return fraction > 0 ? ++price : price;
        }
    
        private int subwayPricy(int km) {
            if (km <= 6) {
                return 3;
            } else if (km > 6 && km < 12) {
                return 4;
    
            } else if (km > 12 && km < 22) {
                return 5;
            } else if (km > 22 && km < 32) {
                return 6;
            } else {
                return 7;
            }
        }
    
        int calculatePrice(int km, int type) {
            if (type == BUS) {
                return busProice(km);
            } else if (type == SUBWAY) {
                return subwayPricy(km);
            }
            return 0;
        }
    
        public static void main(String[] args) {
            PriceCalculator priceCalculator = new PriceCalculator();
            System.out.println(priceCalculator.calculatePrice(12, 1));
        }
    
    }
    
    

    把所有的计算过程放到一个类中,然后通过if-else做判断分类返回,如果再加交通工具,需要在加方法,加if else,这样的耦合性太高,代码臃肿,难以维护,

    1. 使用策略模式
    /**
    * 计费接口
    */
    public interface CalculateStrategy {
    
        int calculatePrice(int km);
    }
    
    

    地铁具体实现

      public class SubwayStrategy implements CalculateStrategy {
        @Override
        public int calculatePrice(int km) {
            if (km <= 6) {
                return 3;
            } else if (km > 6 && km < 12) {
                return 4;
    
            } else if (km > 12 && km < 22) {
                return 5;
            } else if (km > 22 && km < 32) {
                return 6;
            } else {
                return 7;
            }
        }
    }
    

    公交具体实现

    public class BusStrategy implements CalculateStrategy {
    
        @Override
        public int calculatePrice(int km) {
            int extraTotal = km - 10;
            int extraFactor = extraTotal / 5;
            int fraction = extraFactor % 5;
            int price = 1 + extraFactor * 1;
            return fraction > 0 ? ++price : price;
        }
    }
    
    

    动态实现

    public class TranficCalculator {
    
        private CalculateStrategy strategy;
    
        public TranficCalculator(CalculateStrategy strategy) {
            this.strategy = strategy;
        }
    
        public int  calculatorPrice(int km){
            return strategy.calculatePrice(km);
    
        }
    }
    

    总结

    1. 通过以上的对比,清晰的看到二者的区别,前者通过if else来解决问题,虽然实现简单,类型层级单一,但是暴露的问题很多,代码臃肿,逻辑复杂,难以升级和维护,没有结构可言,
    2. 后者通过建立抽象接口,讲不同策略构建成一个具体的策略实现,通过不同策略实现算法替换,在简化逻辑、结构的同时,增强了协同的可读性、稳定性、可扩展性,这对于较为复杂的业务逻辑显得更为直观,扩展也更为方便。

    相关文章

      网友评论

          本文标题:设计模式---策略(Stragety)

          本文链接:https://www.haomeiwen.com/subject/nhfvextx.html