Refactoring-replace Switch statement with state/strategy

Source: Internet
Author: User

/*

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.