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");