Skip to content

Instantly share code, notes, and snippets.

@Naphier
Created December 30, 2015 17:11
Show Gist options
  • Save Naphier/5db3d52635f7361cfc9e to your computer and use it in GitHub Desktop.
Save Naphier/5db3d52635f7361cfc9e to your computer and use it in GitHub Desktop.
Strategy Design Pattern Structure
using System;
namespace StrategyPattern.structure
{
abstract class Strategy
{
public abstract void AlgorithmInterface();
}
class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("ConcreteStrategyA.AlgorithmInterface()");
}
}
class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("ConcreteStrategyB.AlgorithmInterface()");
}
}
class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("ConcreteStrategyC.AlgorithmInterface()");
}
}
class Context
{
private Strategy _strategy;
public Context(Strategy strategy)
{
this._strategy = strategy;
}
public void ContextInterface()
{
_strategy.AlgorithmInterface();
}
}
class MainApp
{
static void Main()
{
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment