Csharp/CSharp Tutorial/Design Patterns/Strategy Pattern
Версия от 15:31, 26 мая 2010; (обсуждение)
Strategy Pattern
using System;
public abstract class Strategy
{
public abstract void MyRule();
public Strategy(){;}
}
public class CorporateStrategy: Strategy
{
public override void MyRule()
{
Console.WriteLine("Corporate.");
}
public CorporateStrategy(){;}
}
public class CreditSalesContext
{
Strategy aStrategy;
public CreditSalesContext (Strategy crStrategy)
{
this.aStrategy = crStrategy;
}
public void BusinessRule()
{
this.aStrategy.MyRule();
}
}
public class DealerStrategy: Strategy
{
public override void MyRule()
{
Console.WriteLine("Dealer.");
}
public DealerStrategy(){;}
}
class Client{
static void Main(string[] args){
CreditSalesContext cr = new CreditSalesContext(new CorporateStrategy());
cr.BusinessRule();
}
}