免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2295 | 回复: 2
打印 上一主题 下一主题

php设计模式 Strategy(策略模式) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-11 15:05 |只看该作者 |倒序浏览
   
php设计模式 Strategy(策略模式)
  1.   1 <?php
  2.   2 /**  
  3.   3  * 策略模式(Strategy.php)
  4.   4  *
  5.   5  * 定义一系列算法,把它们一个个封装起来,并且使它们可相互替换,使用得算法的变化可独立于使用它的客户
  6.   6  *
  7.   7  */
  8.   8
  9.   9 //  ---以下是一系列算法的封闭----
  10. 10 interface CacheTable
  11. 11 {
  12. 12      public function get($key);
  13. 13      public function set($key,$value);
  14. 14      public function del($key);
  15. 15 }
  16. 16
  17.   17 // 不使用缓存
  18. 18 class NoCache implements CacheTable
  19. 19 {
  20. 20     public function __construct(){
  21. 21         echo "Use NoCache<br/>";
  22. 22     }
  23. 23
  24. 24     public function get($key)
  25. 25     {
  26. 26         return false;
  27. 27     }
  28. 28
  29.   29     public function set($key,$value)
  30. 30     {
  31. 31         return true;
  32. 32     }
  33. 33
  34.   34     public function del($key)
  35. 35     {
  36. 36         return false;
  37. 37     }
  38. 38 }
  39. 39
  40.   40 // 文件缓存
  41. 41 class FileCache implements CacheTable
  42. 42 {
  43. 43     public function __construct()
  44. 44     {
  45. 45         echo "Use FileCache<br/>";
  46. 46         // 文件缓存构造函数
  47. 47     }
  48. 48
  49.   49     public function get($key)
  50. 50     {
  51. 51         // 文件缓存的get方法实现
  52. 52     }
  53. 53
  54. 54     public function set($key,$value)
  55. 55     {
  56. 56          // 文件缓存的set方法实现
  57. 57     }
  58. 58
  59. 59     public function del($key)
  60. 60     {
  61. 61          // 文件缓存的del方法实现
  62. 62     }
  63. 63 }
  64. 64  
  65. 65 // TTServer
  66. 66 class TTCache implements CacheTable
  67. 67 {
  68. 68     public function __construct()
  69. 69     {
  70. 70         echo "Use TTCache<br/>";
  71. 71         // TTServer缓存构造函数
  72. 72     }
  73. 73
  74. 74     public function get($key)
  75. 75     {
  76. 76         // TTServer缓存的get方法实现
  77. 77     }
  78. 78  
  79. 79     public function set($key,$value)
  80. 80     {
  81. 81          // TTServer缓存的set方法实现
  82. 82     }
  83. 83
  84. 84     public function del($key)
  85. 85     {
  86. 86          // TTServer缓存的del方法实现
  87. 87     }
  88. 88 }
  89. 89
  90. 90 // -- 以下是使用不用缓存的策略 ------
  91. 91 class Model
  92. 92 {
  93. 93     private $_cache;
  94. 94     public function __construct()
  95. 95     {
  96. 96         $this->_cache = new NoCache();
  97. 97     }
  98. 98  
  99. 99     public function setCache($cache)
  100. 100     {
  101. 101         $this->_cache = $cache;
  102. 102     }
  103. 103 }
  104. 104
  105. 105 class UserModel extends Model
  106. 106 {
  107. 107 }
  108. 108
  109. 109 class PorductModel extends Model
  110. 110 {
  111. 111     public function __construct()
  112. 112     {
  113. 113         $this->_cache = new TTCache();
  114. 114     }
  115. 115 }
  116. 116
  117. 117 // -- 实例一下 ---
  118. 118 $mdlUser = new UserModel();
  119. 119 $mdlProduct = new PorductModel();
  120. 120 $mdlProduct->setCache(new FileCache()); // 改变缓存策略
  121. 121 ?>
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-06-11 23:06 |只看该作者
火速围观!牛人招PHP技术员-出得厅堂,下得厨房,进得洞房!有木有!!有木有!!

http://bbs.phpchina.com/thread-214382-1-1.html   这公司太有才了,做PHP的还要求会武功,找金庸大师学学吧,笑死俺了哈

论坛徽章:
0
3 [报告]
发表于 2011-06-12 16:22 |只看该作者
火速围观!牛人招PHP技术员-出得厅堂,下得厨房,进得洞房!有木有!!有木有!!

http://bbs.phpchina.com/thread-214382-1-1.html   这公司太有才了,做PHP的还要求会武功,找金庸大师学学吧,笑死俺了哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP