Strategy design pattern is a part of behavior design pattern,
It basically isolate the logic from its original class.
And it allow to client to choose 1 best option from its multiple options for same kind of objective
public class StrategyTest
{
public static void MainMethod()
{
var context1 = new Context(new S1());
context1.SetContext();
var context2 = new Context(new S2());
context2.SetContext();
}
}
public abstract class Strategy1
{
public abstract void DoWork();
}
public class S1 : Strategy1
{
public override void DoWork()
{
Console.WriteLine("S1");
}
}
public class S2 : Strategy1
{
public override void DoWork()
{
Console.WriteLine("S2");
}
}
public class Context
{
private Strategy1 _strategy1;
public Context(Strategy1 strategy1)
{
_strategy1 = strategy1;
}
public void SetContext()
{
_strategy1.DoWork();
}
}
It basically isolate the logic from its original class.
And it allow to client to choose 1 best option from its multiple options for same kind of objective
public class StrategyTest
{
public static void MainMethod()
{
var context1 = new Context(new S1());
context1.SetContext();
var context2 = new Context(new S2());
context2.SetContext();
}
}
public abstract class Strategy1
{
public abstract void DoWork();
}
public class S1 : Strategy1
{
public override void DoWork()
{
Console.WriteLine("S1");
}
}
public class S2 : Strategy1
{
public override void DoWork()
{
Console.WriteLine("S2");
}
}
public class Context
{
private Strategy1 _strategy1;
public Context(Strategy1 strategy1)
{
_strategy1 = strategy1;
}
public void SetContext()
{
_strategy1.DoWork();
}
}