策略模式

 策略模式的簡單實現

  1. <?php 
  2. namespace  Strategy; 
  3.  
  4. /** 
  5.  * 抽象算法類 
  6.  * @author struggle 
  7.  * @verison 2012-5-29 
  8.  */ 
  9. abstract class Strategy { 
  10.     /** 
  11.      * 算法方法 
  12.      */ 
  13.     public abstract function algorithmFunction(); 
  14. /** 
  15.  * 具體算法類A 
  16.  * @author struggle 
  17.  * @verison 2012-5-29 
  18.  */ 
  19. class SpecificStrategyA extends  Strategy { 
  20.     /** 
  21.      * 具體算法A的實現 
  22.      */ 
  23.     public function algorithmFunction(){ 
  24.         echo '具體算法A<br>'
  25.     } 
  26. }  
  27. /** 
  28.  * 具體算法類B 
  29.  * @author struggle 
  30.  * @verison 2012-5-29 
  31.  */ 
  32. class SpecificStrategyB extends Strategy { 
  33.     /** 
  34.      * 具體算法B的實現 
  35.      */ 
  36.     public function algorithmFunction(){ 
  37.         echo '具體算法B<br>'
  38.     } 
  39. /** 
  40.  * 具體算法類C 
  41.  * @author struggle 
  42.  * @verison 2012-5-29 
  43.  */ 
  44. class SpecificStrategyC extends  Strategy { 
  45.     /** 
  46.      * 具體算法c的實現 
  47.      */ 
  48.     public function algorithmFunction() { 
  49.         echo '具體算法C<br>'
  50.     } 
  51.  
  52. class Context { 
  53.     /** 
  54.      * 算法對象 
  55.      * @var obj 
  56.      */ 
  57.     private $Strategy ; 
  58.     /** 
  59.      * 設置算法對象 
  60.      * @param Strategy $obj 
  61.      */ 
  62.     public function setStrategy( Strategy $obj){ 
  63.         $this->Strategy = $obj
  64.     } 
  65.      
  66.     public function contextInterface(){ 
  67.         if(!isset($this->Strategy)){ 
  68.            throw new  \Exception('算法對象未設置'); 
  69.         } 
  70.         $this->Strategy->algorithmFunction(); 
  71.     } 
  72.  
  73. $context = new Context(); 
  74. $context->setStrategy(new SpecificStrategyA()); 
  75. $context->contextInterface(); 
  76. $context->setStrategy(new SpecificStrategyB()); 
  77. $context->contextInterface(); 
  78. $context->setStrategy(new SpecificStrategyC()); 
  79. $context->contextInterface(); 

#使用策略與簡單工廠的結合

Context類修改如下:

  1. class Context{ 
  2.     /** 
  3.      * 算法對象 
  4.      * @var obj 
  5.      */ 
  6.     private $Strategy ; 
  7.     /** 
  8.      * 設置算法對象 
  9.      * @param $obj 
  10.      */ 
  11.     public function __construct($type){ 
  12.        switch (strtoupper($type)){ //全部轉換爲大寫 
  13.             case 'A'
  14.                 $this->Strategy = new SpecificStrategyA(); 
  15.                 break
  16.             case 'B'
  17.                 $this->Strategy = new SpecificStrategyB(); 
  18.                 break
  19.             case 'C'
  20.                 $this->Strategy = new SpecificStrategyC(); 
  21.                 break
  22.             default
  23.                 throw new \Exception('未找到算法類 : '.$type); 
  24.        } 
  25.     } 
  26.      
  27.     public function contextInterface(){ 
  28.         if(!isset($this->Strategy)){ 
  29.            throw new  \Exception('算法對象未設置'); 
  30.         } 
  31.         $this->Strategy->algorithmFunction(); 
  32.     } 
  33.  
  34. $context = new Context('A'); 
  35. $context->contextInterface(); 
  36. $context = new Context('B'); 
  37. $context->contextInterface(); 
  38. $context = new Context('c'); 
  39. $context->contextInterface(); 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章