Click here to Skip to main content
11,584,040 members (64,168 online)
Click here to Skip to main content

Tagged as

Strategy method or Switch statement in C#

, 8 Jun 2011 CPOL 22.3K 4
Rate this:
Please Sign up or sign in to vote.
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,
public enum SystemInformation
{
    Default = 0,
    ComputerName,
    ProcessesorName
}
and switch block to do the operation,
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,
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,
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,
public static void StrategyOfSystemInformationSelection(SystemInformation systemDetails)
{
     new Strategy().SystemDetailsProcessDictionary[systemDetails]();
}

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

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,
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)

Share

About the Author


You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid