一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php設計模式 Strategy(策略模式)

php設計模式 Strategy(策略模式)

2019-12-02 13:46PHP教程網 PHP教程

定義一系列算法,把它們一個個封裝起來,并且使它們可相互替換,使用得算法的變化可獨立于使用它的客戶

抽象策略(Strategy)角色:定義所有支持的算法的公共接口。通常是以一個接口或抽象來實現。Context使用這個接口來調用其ConcreteStrategy定義的算法。

具體策略(ConcreteStrategy)角色:以Strategy接口實現某具體算法。

環境(Context)角色:持有一個Strategy類的引用,用一個ConcreteStrategy對象來配置

php設計模式 Strategy(策略模式)

核心代碼

  1. <?php 
  2. interface Strategy { // 抽象策略角色,以接口實現 
  3.   public function algorithmInterface(); // 算法接口 
  4.   
  5. class ConcreteStrategyA implements Strategy { // 具體策略角色A  
  6.   public function algorithmInterface() {} 
  7.   
  8. class ConcreteStrategyB implements Strategy { // 具體策略角色B  
  9.   public function algorithmInterface() {} 
  10.   
  11. class ConcreteStrategyC implements Strategy { // 具體策略角色C 
  12.   public function algorithmInterface() {} 
  13.   
  14. class Context { // 環境角色 
  15.   private $_strategy; 
  16.   public function __construct(Strategy $strategy) { 
  17.     $this->_strategy = $strategy; 
  18.   }  
  19.   public function contextInterface() { 
  20.     $this->_strategy->algorithmInterface(); 
  21.   } 
  22.   
  23. // client 
  24. $strategyA = new ConcreteStrategyA(); 
  25. $context = new Context($strategyA); 
  26. $context->contextInterface(); 
  27.   
  28. $strategyB = new ConcreteStrategyB(); 
  29. $context = new Context($strategyB); 
  30. $context->contextInterface(); 
  31.   
  32. $strategyC = new ConcreteStrategyC(); 
  33. $context = new Context($strategyC); 
  34. $context->contextInterface(); 

其他代碼

  1. <?php  
  2. /**  
  3. 策略模式(Strategy.php)  
  4.  
  5. * 定義一系列算法,把它們一個個封裝起來,并且使它們可相互替換,使用得算法的變化可獨立于使用它的客戶  
  6.  
  7. */ 
  8.   
  9. // ---以下是一系列算法的封閉----  
  10. interface CacheTable  
  11. {  
  12. public function get($key);  
  13. public function set($key,$value);  
  14. public function del($key);  
  15. }  
  16.   
  17. // 不使用緩存  
  18. class NoCache implements CacheTable  
  19. {  
  20. public function __construct(){  
  21. echo "Use NoCache<br/>";  
  22. }  
  23.   
  24. public function get($key)  
  25. {  
  26. return false;  
  27. }  
  28.   
  29. public function set($key,$value)  
  30. {  
  31. return true;  
  32. }  
  33.   
  34. public function del($key)  
  35. {  
  36. return false;  
  37. }  
  38. }  
  39.   
  40. // 文件緩存  
  41. class FileCache implements CacheTable  
  42. {  
  43. public function __construct()  
  44. {  
  45. echo "Use FileCache<br/>";  
  46. // 文件緩存構造函數  
  47. }  
  48.   
  49. public function get($key)  
  50. {  
  51. // 文件緩存的get方法實現  
  52. }  
  53.   
  54. public function set($key,$value)  
  55. {  
  56. // 文件緩存的set方法實現  
  57. }  
  58.   
  59. public function del($key)  
  60. {  
  61. // 文件緩存的del方法實現  
  62. }  
  63. }  
  64.   
  65. // TTServer  
  66. class TTCache implements CacheTable  
  67. {  
  68. public function __construct()  
  69. {  
  70. echo "Use TTCache<br/>";  
  71. // TTServer緩存構造函數  
  72. }  
  73.   
  74. public function get($key)  
  75. {  
  76. // TTServer緩存的get方法實現  
  77. }  
  78.   
  79. public function set($key,$value)  
  80. {  
  81. // TTServer緩存的set方法實現  
  82. }  
  83.   
  84. public function del($key)  
  85. {  
  86. // TTServer緩存的del方法實現  
  87. }  
  88. }  
  89.   
  90. // -- 以下是使用不用緩存的策略 ------  
  91. class Model  
  92. {  
  93. private $_cache;  
  94. public function __construct()  
  95. {  
  96. $this->_cache = new NoCache();  
  97. }  
  98.   
  99. public function setCache($cache)  
  100. {  
  101. $this->_cache = $cache;  
  102. }  
  103. }  
  104.   
  105. class UserModel extends Model  
  106. {  
  107. }  
  108.   
  109. class PorductModel extends Model  
  110. {  
  111. public function __construct()  
  112. {  
  113. $this->_cache = new TTCache();  
  114. }  
  115. }  
  116.   
  117. // -- 實例一下 ---  
  118. $mdlUser = new UserModel();  
  119. $mdlProduct = new PorductModel();  
  120. $mdlProduct->setCache(new FileCache()); // 改變緩存策略  
  121. ?> 

具體的大家可以多關注一下服務器之家以前發布的文章

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美在线视频 一区二区 | 男生操女生动态图 | 99热r| 国产在线观看人成激情视频 | 喷奶水榨乳ova动漫无修 | 国产精品亚洲精品日韩已满 | 白丝爆动漫羞羞动漫网站 | 国产午夜成人无码免费看 | 久久精品国产久精国产果冻传媒 | 99精品免费在线 | gay小太正初精 | 国产香蕉97碰碰久久人人 | 亚洲午夜久久久久国产 | 毛片免 | 99热这里只有精品一区二区三区 | 国内精品91东航翘臀女神在线 | 希岛爱理作品在线观看 | 午夜AV内射一区二区三区红桃视 | 古装床戏做爰无遮挡三级 | 欧美 亚洲 综合 卡通 另类 区 | 俄罗斯引擎首页进入 | 日日插插| 美女全身体光羞羞漫画 | 成人欧美1314www色视频 | 高清色黄毛片一级毛片 | 国产亚洲玖玖玖在线观看 | 精品AV亚洲乱码一区二区 | 乳女教师欲乱动漫无修版动画3d | 日韩精品亚洲一级在线观看 | 日本漫画大全之工之口 | 久久艹影院 | 四虎影视永久在线精品免费 | 精品国产品香蕉在线观看 | 高清不卡一区 | 果冻传媒在线完整免费观 | 国模娜娜一区二区三区 | 国产高清在线播放刘婷91 | 久久国产36精品色熟妇 | 国产成人精品一区二三区在线观看 | 网www天堂资源在线 王淑兰与铁柱全文免费阅读 | 免费看视频 |