Csharp/CSharp Tutorial/Design Patterns/Strategy Pattern — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 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();
    }
  }