Refactoring - replace Switch statement with state/strategy

/*

Author: Jiangong SUN

*/


switch statements is procedural structure, not object programming, it can be replaced by strategy pattern with implementation of interface/abstract strategy, with concrete strategies.

public class SwitchToReplace
    {
        public static void Main()
        {
            Console.WriteLine(Get(Instruments.Telephone));
            Console.WriteLine(Get(Instruments.Mobile));
            Console.WriteLine(Get(Instruments.Television));
            Console.WriteLine(Get(Instruments.Guitar));
            Console.ReadLine();
        }

        public enum Instruments
        {
            Telephone,
            Mobile,
            Television,
            Guitar
        }

        public static string Get(Instruments instruments)
        {
            string value;
            switch (instruments)
            {
                case Instruments.Telephone:
                    Console.WriteLine("Case 1");
                    value = "Telephone";
                    break;
                case Instruments.Mobile:
                    Console.WriteLine("Case 2");
                    value = "Mobile";
                    break;
                case Instruments.Television:
                    Console.WriteLine("Case 3");
                    value = "Television";
                    break;
                default:
                    Console.WriteLine("Default case");
                    value = "Default case";
                    break;
            }
            return value;
        }
    }

Replace Switch statement with State/Strategy pattern


    public class SwitchReplaced
    {
        public enum InstrumentEnum
        {
            Telephone,
            Mobile,
            Television,
            Guitar
        }
        //define an abstract class and its method signature
        public abstract class Instrument
        {
            public abstract string GetInstrumentName();
        }
        //inherit abstract class and override it's method
        public class InstrumentTelephone : Instrument
        {
            public override string GetInstrumentName()
            {
                Console.WriteLine("Case 1");
                return "Telephone";
            }
        }
        public class InstrumentMobile : Instrument
        {
            public override string GetInstrumentName()
            {
                Console.WriteLine("Case 2");
                return "Mobile";
            }
        }
        public class InstrumentTelevision : Instrument
        {
            public override string GetInstrumentName()
            {
                Console.WriteLine("Case 3");
                return "Television";
            }
        }
        public class InstrumentGuitar : Instrument
        {
            public override string GetInstrumentName()
            {
                Console.WriteLine("Case 4");
                return "Guitar";
            }
        }
        public class InstrumentContext
        {
            private readonly Instrument _instrumentInstance;
            //put abstract class Instrument's instance in constructor, assign it to local variable
            public InstrumentContext(Instrument instrument)
            {
                _instrumentInstance = instrument;
            }
            //Invoke method of concrete strategy
            public string InvokeGetInstrumentName()
            {
                return _instrumentInstance.GetInstrumentName();
            }
        }
        //Extract same calling code in a single method
        public string InstrumentInfo(Instrument instrument)
        {
            InstrumentContext context = new InstrumentContext(instrument);
            return context.InvokeGetInstrumentName();
        }

        public static void Main()
        {
            SwitchReplaced sr = new SwitchReplaced();
            //call
            Console.WriteLine(sr.InstrumentInfo(new InstrumentGuitar()));
            Console.WriteLine(sr.InstrumentInfo(new InstrumentMobile()));
            Console.ReadLine();
        }
    }


I hope this post can do help to you! enjoy coding!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值