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