Strategy Pattern : Strategy Pattern « Design Patterns « C# / CSharp Tutorial






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();
    }
  }








28.8.Strategy Pattern
28.8.1.Strategy Pattern