程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Refactoring - replace Switch statement with state/strategy

Refactoring - replace Switch statement with state/strategy

編輯:C#入門知識

/*

Author: Jiangong SUN

*/

 


Extract Method

Move Method

Replace Temp by Query

Replace type code with state/strategy

 


Strategy Statement to replace:


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 Instruments
        {
            Telephone,
            Mobile,
            Television,
            Guitar
        }
        public abstract class Instrument
        {
            public abstract string GetInfo();
        }
        public class InstrumentTelephone : Instrument
        {
            public override string GetInfo()
            {
                Console.WriteLine("Case 1");
                return "Telephone";
            }
        }
        public class InstrumentMobile : Instrument
        {
            public override string GetInfo()
            {
                Console.WriteLine("Case 2");
                return "Mobile";
            }
        }
        public class InstrumentTelevision : Instrument
        {
            public override string GetInfo()
            {
                Console.WriteLine("Case 3");
                return "Television";
            }
        }
        public class InstrumentGuitar : Instrument
        {
            public override string GetInfo()
            {
                Console.WriteLine("Case 4");
                return "Guitar";
            }
        }
        public static string Get(Instruments instruments)
        {
            string value = string.Empty;
            if (instruments == Instruments.Telephone) value= new InstrumentTelephone().GetInfo();
            if (instruments == Instruments.Mobile) value= new InstrumentMobile().GetInfo();
            if (instruments == Instruments.Television) value= new InstrumentTelevision().GetInfo();
            if (instruments == Instruments.Guitar) value= new InstrumentGuitar().GetInfo();
            return value;
        }
        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();
        }
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved