There are different ways to define business logic and execution rules of those business logic in a application.
Class
is one of the common ways to define business logic and different conditional operators to define the execution rule.
Polymorphic
is one of the ways to execute different business logic in different situations. For example, if
IGeometry
defines a method named
Draw()
then it is possible to implement three different
classes
Circle
,
Rectangle
and
Trinagle
implement the
IGeometry
interface. So the
Draw()
method implementation would be different depending on the situation. So when user makes an instance of
Circle
type like below:
IGeometry objects = new Circle();
and to call
Draw()
method for example,
objects.Draw()
then the objects will draw a circle.
We could define some Strategy to select
type
and depeding on the Strategy, system will
return
appropriate
type
. First of all, we will define an
enum
which will have all our business rules definition as items and a repository which will have mapping of our business rules definitions and related types.
public static class Strategy
{
public enum StrategyOfDisplayProcessorSelection
{
StrategyOne,
StrategyTwo
}
public static Dictionary<StrategyOfDisplayProcessorSelection, IDisplayProcessor> TypeSelectionStrategy = new Dictionary<StrategyOfDisplayProcessorSelection, IDisplayProcessor>()
{
{StrategyOfDisplayProcessorSelection.StrategyOne, new Lazy<DisplayProcessorOne>().Value},
{StrategyOfDisplayProcessorSelection.StrategyTwo, new Lazy<DisplayProcessorTwo>().Value},
};
}
In the above code,
enum StrategyOfDisplayProcessorSelection
defines our business rules and
TypeSelectionStrategy
is the mapping repository in this case
StrategyOne
maps
DisplayProcessorOne
,
StrategyTwo
maps
DisplayProcessorTwo
. This repository defines using
Dictionary
,
enum
StrategyOfDisplayProcessorSelection
as
key
and
IDisplayProcessor
as
type
. When the value from
TypeSelectionStrategy
will be retrieved, it will be the same as mapping type, because
DisplayProcessorOne
and
DisplayProcessorTwo
implements
IDisplayProcessor
.
The
interface
and implementation
classes
are below:
public interface IDisplayProcessor
{
void DisplayUsingComponentCable(string whichTele);
void DisplayUsingHdmiCable(string whichTele);
}
public class DisplayProcessorOne : IDisplayProcessor
{
public void DisplayUsingComponentCable(string whichTele)
{
Console.WriteLine(whichTele);
}
public void DisplayUsingHdmiCable(string whichTele)
{
Console.WriteLine(whichTele);
}
}
public class DisplayProcessorTwo : IDisplayProcessor
{
public void DisplayUsingComponentCable(string whichTele)
{
Console.WriteLine(whichTele);
}
public void DisplayUsingHdmiCable(string whichTele)
{
Console.WriteLine(whichTele);
}
}
Usages of
Strategy
are as below:
var displayProcessorOne =Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyOne];
var displayProcessorTwo =Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyTwo];
Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyOne].DisplayUsingComponentCable("Sony Bravia");
Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyOne].DisplayUsingHdmiCable("Samsung");
Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyTwo].DisplayUsingComponentCable("Sony Bravia");
Strategy.TypeSelectionStrategy[Strategy.StrategyOfDisplayProcessorSelection.StrategyTwo].DisplayUsingHdmiCable("Samsung");