C#面向对象设计模式纵横谈——23 Strategy策略模式

主讲:李建忠

来源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx

 

clip_image002

clip_image004

clip_image006

clip_image008

 

   1:  enum CartType
   2:  {
   3:      A,
   4:      B,
   5:      C
   6:  }
   7:   
   8:  public interface IProcessStrategy    //表达算法抽象
   9:  {
  10:      void Process();
  11:  }
  12:   
  13:  public ProcessStrategyA: IProcessStrategy 
  14:  {
  15:      public void Process()
  16:      {
  17:          //...
  18:      }
  19:  }
  20:   
  21:  public ProcessStrategyB: IProcessStrategy 
  22:  {
  23:      public void Process()
  24:      {
  25:          //...
  26:      }
  27:  }
  28:   
  29:  public ProcessStrategyC: IProcessStrategy 
  30:  {
  31:      public void Process()
  32:      {
  33:          //...
  34:      }    
  35:  }
  36:   
  37:  class Cart
  38:  {
  39:      IProcessStrategy processStrategy;
  40:   
  41:      public Cart(IProcessStrategy processStrategy)
  42:      {
  43:          this.processStrategy=processStrategy;
  44:      }
  45:   
  46:      public void Somemethod()
  47:      {
  48:          //...
  49:          processStrategy.Process();
  50:      }
  51:  }
  52:   
  53:  class App
  54:  {
  55:      public static void Main()
  56:      {
  57:          Cart cart=new Cart(new ProcessStrategyC());
  58:      }
  59:  }

 

clip_image010

clip_image012

你可能感兴趣的:(strategy)