/*
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();
}
}