using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
class
Program
{
static
void
Main(string[]
args)
{
/*
STRATEGY PATTERN (POLICY PATTERN):
*
Strategy lets the algorithm vary independently
* from clients that use it.
* from clients that use it.
*
When the program is running, we can switch between different
* type of algorithms
* type of algorithms
*
For instance, a class that performs validation on incoming data
*
may use a strategy pattern to select a validation
* algorithm based on the type of data,
* the source of the data, user choice,
* algorithm based on the type of data,
* the source of the data, user choice,
*
or other discriminating factors. These factors are not known
* for each case until run-time, and may require
* radically different validation to be performed.
* for each case until run-time, and may require
* radically different validation to be performed.
*/
ICalculate
calculate = null;
while
(true)
{
try
{
Console.WriteLine("Choose
an operation! ( 1 = Plusus, 2 = Minus )");
StrategyType
strategyType = (StrategyType)Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
the first number");
int
value1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
the second number");
int
value2 = Convert.ToInt32(Console.ReadLine());
switch
(strategyType)
{
case
StrategyType.Plussus:
calculate
= new
Plussus();
Console.WriteLine("Operation:
Plusus, Result: "
+ calculate.Calculate(value1, value2));
break;
case
StrategyType.Minus:
calculate
= new
Minus();
Console.WriteLine("Operation:
Minus, Result: "
+ calculate.Calculate(value1, value2));
break;
}
}
catch
(Exception)
{
Console.WriteLine("Enter
a valid number!");
}
}
}
}
}
*********************
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
//The
interface for the strategies
public
interface
ICalculate
{
//Method
is defined in the interface
int
Calculate(int
value1, int
value2);
}
}
*********************
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
//Strategy:
Minus
public
class
Minus
: ICalculate
{
public
int
Calculate(int
value1, int
value2)
{
//The
minus logis is defined
return
value1 - value2;
}
}
}
*********************
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
//Strategy:
Plussus
public
class
Plussus:ICalculate
{
public
int
Calculate(int
value1, int
value2)
{
//The
plussus logic is defined
return
value1 + value2;
}
}
}
*********************
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
enum
StrategyType
{
Plussus
= 1,
Minus
= 2
}
}
*********************
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DesignPattern_StrategyPattern
{
//The
client
public
class
CalculateClient
{
private
ICalculate
calculate;
public
CalculateClient(ICalculate
strategy)
{
this.calculate
= strategy;
}
public
int
Calculate(int
value1, int
value2)
{
return
calculate.Calculate(value1, value2);
}
}
}
Comments
Post a Comment