• 31天重构学习笔记11. 使用策略类


    概念:本文中的“使用策略类” 是指用设计模式中的策略模式来替换原来的switch case和if else语句,这样可以解开耦合,同时也使维护性和系统的可扩展性大大增强。

    正文:如下面代码所示,ClientCode 类会更加枚举State的值来调用ShippingInfo 的不同方法,但是这样就会产生很多的判断语句,如果代码量加大,类变得很大了的话,维护中改动也会变得很大,每次改动一个地方,都要对整个结构进行编译(假如是多个工程),所以我们想到了对它进行重构,剥开耦合。

    namespace LosTechies.DaysOfRefactoring.SwitchToStrategy.Before
    {
        public class ClientCode
        {
            public decimal CalculateShipping()
            {
                ShippingInfo shippingInfo = new ShippingInfo();
                return shippingInfo.CalculateShippingAmount(State.Alaska);
            }
        }
    
        public enum State
        {
            Alaska,
            NewYork,
            Florida
        }
    
        public class ShippingInfo
        {
            public decimal CalculateShippingAmount(State shipToState)
            {
                switch (shipToState)
                {
                    case State.Alaska:
                        return GetAlaskaShippingAmount();
                    case State.NewYork:
                        return GetNewYorkShippingAmount();
                    case State.Florida:
                        return GetFloridaShippingAmount();
                    default:
                        return 0m;
                }
            }
    
            private decimal GetAlaskaShippingAmount()
            {
                return 15m;
            }
    
            private decimal GetNewYorkShippingAmount()
            {
                return 10m;
            }
    
            private decimal GetFloridaShippingAmount()
            {
                return 3m;
            }
        }
    }

    重构后的代码如下所示,抽象出一个IShippingCalculation 接口,然后把ShippingInfo 类里面的GetAlaskaShippingAmount、GetNewYorkShippingAmount、GetFloridaShippingAmount三个方法分别提炼成三个类,然后继承自IShippingCalculation 接口,这样在调用的时候就可以通过IEnumerable<IShippingCalculation> 来解除之前的switch case语句,这和IOC的做法颇为相似。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace LosTechies.DaysOfRefactoring.SwitchToStrategy.After_WithIoC
    {
        public interface IShippingInfo
        {
            decimal CalculateShippingAmount(State state);
        }
    
        public class ClientCode
        {
            [Inject]
            public IShippingInfo ShippingInfo { get; set; }
    
            public decimal CalculateShipping()
            {
                return ShippingInfo.CalculateShippingAmount(State.Alaska);
            }
        }
    
        public enum State
        {
            Alaska,
            NewYork,
            Florida
        }
    
        public class ShippingInfo : IShippingInfo
        {
            private IDictionary<State, IShippingCalculation> ShippingCalculations { get; set; }
    
            public ShippingInfo(IEnumerable<IShippingCalculation> shippingCalculations)
            {
                ShippingCalculations = shippingCalculations.ToDictionary(calc => calc.State);
            }
    
            public decimal CalculateShippingAmount(State shipToState)
            {
                return ShippingCalculations[shipToState].Calculate();
            }
        }
    
        public interface IShippingCalculation
        {
            State State { get; }
            decimal Calculate();
        }
    
        public class AlaskShippingCalculation : IShippingCalculation
        {
            public State State { get { return State.Alaska; } }
    
            public decimal Calculate()
            {
                return 15m;
            }
        }
    
        public class NewYorkShippingCalculation : IShippingCalculation
        {
            public State State { get { return State.NewYork; } }
    
            public decimal Calculate()
            {
                return 10m;
            }
        }
    
        public class FloridaShippingCalculation : IShippingCalculation
        {
            public State State { get { return State.Florida; } }
    
            public decimal Calculate()
            {
                return 3m;
            }
        }
    } 

    总结:这种重构在设计模式当中把它单独取了一个名字——策略模式,这样做的好处就是可以隔开耦合,以注入的形式实现功能,这使增加功能变得更加容易和简便,同样也增强了整个系统的稳定性和健壮性。

  • 相关阅读:
    C++ Primer读书笔记
    谨慎使用多线程中的fork
    C++中多线程与Singleton的那些事儿
    浅谈指针的比较
    条件变量的陷阱与思考
    2014年终总结
    循环队列的一种实现模型
    react-native使用jest、enzyme进行单元测试
    富文本编辑器开发原理
    模拟实现单元测试中的异步测试
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/2892588.html
Copyright © 2020-2023  润新知