Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Tip/Trick

Strategy method or Switch statement in C#

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
8 Jun 2011CPOL1 min read 45.2K   5   5
In programming, use of conditional operation is one of the common scenario. if, switch are few of those control statements which help us to do the conditional operation. If we think about a situation, in where we need to do multiple tasks for each of the condition for example, if user select display Computer details then we need to write few statements to display Computer information or if they select to display Processor information also require to write few statements. To make code elegant we will probably move all those statements on its own method block and run those methods based on the condition.

To describe the situation I created an enum which defines our conditions,
C#
public enum SystemInformation
{
    Default = 0,
    ComputerName,
    ProcessesorName
}

and switch block to do the operation,
C#
public static void ProcessSystemInformation(SystemInformation systemInformation)
{
   switch (systemInformation)
   {
       case SystemInformation.ComputerName:
         Console.WriteLine("My Computer");
         break;
       case SystemInformation.ProcessesorName:
         Console.WriteLine("My Processor");
         break;
       case SystemInformation.Default:
         break;
   }
}

So instead of doing this we could create a Strategy class which will implement the strategy of our business logic selection process. To do that, First of all we need to map the Enum values into an Dictionary, this Dictionary will hold values of an Enum as keys and Action as values. In here I use Action because my method will not accept any parameter and will not return anything. It is mentioned that we could use Func<t1..tn,tresult> if the method requires any input or ouput.

The code block for the Dictionary is below,
C#
public Dictionary<SystemInformation, Action> SystemDetailsProcessDictionary
{
    get
    {
       return new Dictionary<SystemInformation, Action>() 
         {
           { SystemInformation.ComputerName,  DisplayComputerName },
           { SystemInformation.ProcessesorName, DisplayProcessesorName },
           { SystemInformation.Default, ()=>{ } }
         };
     }
}

and the method blocks of DisplayComputerName and DisplayProcessorName is below,
C#
public void DisplayComputerName()
{
   Console.WriteLine("My Computer");
}

public void DisplayProcessesorName()
{
   Console.WriteLine("My Processesor");
}

To make the code simple I didn't put many statement in the method blocks.

To replace the Switch we need to use following Strategy method,
C#
public static void StrategyOfSystemInformationSelection(SystemInformation systemDetails)
{
     new Strategy().SystemDetailsProcessDictionary[systemDetails]();
}


or if we want to pass string value instead of Enum values.

XML
public static void StrategyOfSystemInformationSelection(string data)
{
    SystemInformation systemDetails = Enum.TryParse<SystemInformation>(data, true, out systemDetails) ? systemDetails : default(SystemInformation);
    new Strategy().SystemDetailsProcessDictionary[systemDetails]();
}

To test the code,
SQL
Strategy.StrategyOfSystemInformationSelection(SystemInformation.ComputerName);
Strategy.StrategyOfSystemInformationSelection(SystemInformation.ProcessesorName);
Strategy.StrategyOfSystemInformationSelection("ComputerName");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
GeneralReason for my vote of 5 I think this is really clever. Redu... Pin
Fournier alexandre14-Jun-11 2:20
Fournier alexandre14-Jun-11 2:20 
GeneralRe: Reason for my vote of 5I think this is really clever. Redu... Pin
Mohammad A Rahman16-May-12 14:44
Mohammad A Rahman16-May-12 14:44 
GeneralNice one +5,,,, but one suggestion.... why should we create ... Pin
Pritesh Aryan11-Jun-11 1:10
Pritesh Aryan11-Jun-11 1:10 
GeneralRe: Thanks, I will improve that. Pin
Mohammad A Rahman11-Jun-11 3:24
Mohammad A Rahman11-Jun-11 3:24 
GeneralReason for my vote of 5 Very informative and makes me think ... Pin
gerkims8-Jun-11 19:34
gerkims8-Jun-11 19:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.