经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
PHP设计模式的策略,适配器和观察者模式详解
来源:jb51  时间:2022/3/14 10:01:58  对本文有异议

策略模式

特点

定义一系列算法封装起来, 让他们可以相互替代,策略模式提供了管理相关算法族的办法, 提供了可以体会继承关系的棒法, 避免使用多重条件转移语句

实现

  1. <?php
  2. abstract class Strategy
  3. {
  4. abstract function goSchool();
  5. }
  6. class Run extends Strategy
  7. {
  8. public function goSchool() {
  9. echo "走路去学校";
  10. }
  11. }
  12. class Subway extends Strategy
  13. {
  14. public function goSchool() {
  15. echo "地铁去学校";
  16. }
  17. }
  18. class Bike extends Strategy
  19. {
  20. public function goSchool() {
  21. echo "公交去学校";
  22. }
  23. }
  24. class GoSchoolContext
  25. {
  26. protected $_stratege;
  27. public function __construct($stratege) {
  28. $this->_stratege = $stratege;
  29. }
  30. public function goSchool()
  31. {
  32. $this->_stratege->goSchool();
  33. }
  34. }
  35. $traget = new Run();
  36. $obj = new GoSchoolContext($traget);
  37. $obj->goSchool();

适配器模式

特点

需要的东西在面前,但却不能用,而短时间又无法改造它,于是就想办法适配

实现

  1. // 适配器
  2. interface Charget
  3. {
  4. public function putCharget();
  5. }
  6. class China implements Charget
  7. {
  8. private $v = 220;
  9. public function putCharget()
  10. {
  11. return $this->v;
  12. }
  13. }
  14. class Adper extends China
  15. {
  16. public function putCharget() {
  17. return parent::putCharget() / 2 + 10;
  18. }
  19. }
  20. class Phone
  21. {
  22. public function charge(Charget $charge)
  23. {
  24. if ($charge->putCharget() != "120") {
  25. echo "不能充电";
  26. } else {
  27. echo "能充电";
  28. }
  29. }
  30. }
  31. $china = new China();
  32. $adper = new Adper();
  33. $phone = new Phone();
  34. $phone->charge($adper);

观察者模式

特点

当一个对象状态发生变化时, 依赖他的对象全部收到通知, 并主动更新。观察者模式实现了低耦合, 非侵入式的通知与更新机制。

实现

  1. <?php
  2. // 主题接口
  3. interface Subject
  4. {
  5. public function register(Observer $observer);
  6. }
  7. // 观察者接口
  8. interface Observer
  9. {
  10. public function watch();
  11. }
  12. // 主题
  13. class WatchAction implements Subject
  14. {
  15. public $_observers = [];
  16. public function register(\Observer $observer) {
  17. $this->_observers[] = $observer;
  18. }
  19. public function notify()
  20. {
  21. foreach($this->_observers as $object) {
  22. $object->watch();
  23. }
  24. }
  25. }
  26. // 观察者
  27. class Cat1 implements Observer{
  28. public function watch(){
  29. echo "Cat1 watches TV<hr/>";
  30. }
  31. }
  32. class Dog1 implements Observer{
  33. public function watch(){
  34. echo "Dog1 watches TV<hr/>";
  35. }
  36. }
  37. class People implements Observer{
  38. public function watch(){
  39. echo "People watches TV<hr/>";
  40. }
  41. }
  42. $action = new WatchAction();
  43. $action->register(new Cat1());
  44. $action->register(new People());
  45. $action->register(new Dog1());
  46. $action->notify();

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注w3xue的更多内容! 

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号