当前位置:首页 > PHP教程 > php面向对象 > 列表

深入分析PHP设计模式

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-14 16:08:16 浏览: 评论:0 

这篇文章主要介绍了PHP设计模式的相关知识,文中讲解非常细致,代码帮助大家更好的理解学习,感兴趣的朋友可以参考下。

1、单例模式

一个类,只能允许有一个对象存在

  1. <?php 
  2. class test{ 
  3.   protected function __construct(){ 
  4.   } 
  5.  
  6.   public static function getInstance(){ 
  7.     $_test = new test(); 
  8.     return $_test; 
  9.   } 
  10. } 
  11. $test = test::getInstance(); 
  12. var_dump($test); 
  13. ?> 

2、工厂模式

工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么,工厂模式主要用于解耦。

把对象的创建和使用的过程分开,比如: ClassA 调用 ClassB,那么 ClassA 只调用ClassB 的方法,至于实例化 ClassB 则在工厂内实现。这样既减少了代码的重复使用,也方便对 ClassB 的后期维护。

如果 ClassB 实例化过程很复杂,使用简单工厂模式就会发现外部无需关注复杂的实例化,只管调用 ClassB 的方法即可,减少错误

  1. interface mysql{  
  2.   public function connect(); 
  3. } 
  4.    
  5. class mysqli2 implements mysql{ 
  6.   public function connect(){ 
  7.     echo 'mysqli'; 
  8.   } 
  9. } 
  10.    
  11. class pdo2 implements mysql{ 
  12.   public function connect(){ 
  13.     echo 'pdo'; 
  14.   } 
  15. } 
  16.  
  17. class mysqlFactory{ 
  18.   static public function factory($class_name){ 
  19.     return new $class_name(); 
  20.   } 
  21. } 
  22. $obj = mysqlFactory::factory('pdo2'); 
  23. $obj->connect(); 

3、注册模式

注册模式,解决全局共享和交换对象。已经创建好的对象,挂在到某个全局可以使用的数组上,在需要使用的时候,直接从该数组上获取即可。将对象注册到全局的树上。任何地方直接去访问。

  1. <?php 
  2. class Register 
  3. { 
  4.     protected static $objects; 
  5.     function set($alias,$object)//将对象注册到全局的树上 
  6.     { 
  7.       self::$objects[$alias]=$object;//将对象放到树上 
  8.     } 
  9.     static function get($name){ 
  10.       return self::$objects[$name];//获取某个注册到树上的对象 
  11.      } 
  12.   function _unset($alias) 
  13.   { 
  14.      unset(self::$objects[$alias]);//移除某个注册到树上的对象。 
  15.     } 
  16. } 
  17.  
  18. \Auto\Register::set('single',$single); 
  19. $single = \Auto\Register::get('single'); 
  20. var_dump($single); 

4、适配器模式

将一个类的接口转换成客户希望的另外一个接口。

  1. //目标角色 
  2. interface Aims 
  3. { 
  4.   public function newMethod1(); 
  5.   public function newMethod2(); 
  6. } 
  7.    
  8. //需要被适配的类(Adaptee) 
  9. Class Man 
  10. { 
  11.   public function oldMethod1() 
  12.   { 
  13.     echo 'man'; 
  14.   } 
  15.    
  16.   public function oldMethod2() 
  17.   { 
  18.     echo '男人'; 
  19.   } 
  20. } 
  21.    
  22. //需要被适配的类(Adaptee) 
  23. Class Woman 
  24. { 
  25.   public function oldMethod1() 
  26.   { 
  27.     echo 'woman'; 
  28.   } 
  29.    
  30.   public function oldMethod2() 
  31.   { 
  32.     echo '女人'; 
  33.   } 
  34. } 
  35.    
  36. //适配器, 
  37. Class Adapters implements Aims 
  38. { 
  39.   private $adaptee; 
  40.   public function __construct($adaptee) 
  41.   { 
  42.     $this->adaptee = $adaptee; 
  43.   } 
  44.    
  45.   public function newMethod1() 
  46.   { 
  47.     //以少量的代码对被适配者作出适配 
  48.     echo 'sex :'; 
  49.     $this->adaptee->oldMethod1(); 
  50.   } 
  51.    
  52.   public function newMethod2() 
  53.   { 
  54.     echo 'sex name :'; 
  55.     $this->adaptee->oldMethod2(); 
  56.   } 
  57. } 
  58.    
  59. $adapter1 = new Adapters(new Man); 
  60. $adapter1->newMethod1(); 
  61. $adapter2 = new Adapters(new Woman); 
  62. $adapter2->newMethod2(); 

5、策略模式

这是一个男人和女人的问题,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境。

UserStrategy.php

  1. <?php 
  2. /* 
  3.  * 声明策略文件的接口,约定策略包含的行为。 
  4.  */ 
  5. interface UserStrategy 
  6. { 
  7.   function showAd(); 
  8.   function showCategory(); 
  9. } 
  10.  
  11. FemaleUser.php 
  12. <?php 
  13. class FemaleUser implements UserStrategy 
  14. { 
  15.   function showAd(){ 
  16.     echo "2016冬季女装"; 
  17.   } 
  18.   function showCategory(){ 
  19.     echo "女装"; 
  20.   } 
  21. } 
  22.  
  23. MaleUser.php 
  24. <?php 
  25. class MaleUser implements UserStrategy 
  26. { 
  27.   function showAd(){ 
  28.     echo "IPhone6s"; 
  29.   } 
  30.   function showCategory(){ 
  31.     echo "电子产品"; 
  32.   } 
  33. } 
  34.  
  35. Page.php//执行文件 
  36. <?php 
  37. require_once 'Loader.php'; 
  38. class Page 
  39. { 
  40.   protected $strategy; 
  41.   function index(){ 
  42.     echo "AD"; 
  43.     $this->strategy->showAd(); 
  44.     echo "<br>"; 
  45.     echo "Category"; 
  46.     $this->strategy->showCategory(); 
  47.     echo "<br>"; 
  48.   } 
  49.   function setStrategy(UserStrategy $strategy){ 
  50.     $this->strategy=$strategy; 
  51.   } 
  52. } 
  53.  
  54. $page = new Page(); 
  55. if(isset($_GET['male'])){ 
  56.   $strategy = new MaleUser(); 
  57. }else { 
  58.   $strategy = new FemaleUser(); 
  59. } 
  60. $page->setStrategy($strategy); 
  61. $page->index(); 

6、原型模式

不常用,大的对象类才使用,表现在clone

7、观察者模式

从面向过程的角度来看,首先是观察者向主题注册,注册完之后,主题再通知观察者做出相应的操作,整个事情就完了。

  1. /** 
  2.  * 事件产生类 
  3.  * Class EventGenerator 
  4.  */ 
  5. abstract class EventGenerator 
  6. { 
  7.   private $ObServers = []; 
  8.  
  9.   //增加观察者 
  10.   public function add(ObServer $ObServer) 
  11.   { 
  12.     $this->ObServers[] = $ObServer; 
  13.   } 
  14.  
  15.   //事件通知 
  16.   public function notify() 
  17.   { 
  18.     foreach ($this->ObServers as $ObServer) { 
  19.       $ObServer->update(); 
  20.     } 
  21.   } 
  22.  
  23. } 
  24.  
  25. /** 
  26.  * 观察者接口类 
  27.  * Interface ObServer 
  28.  */ 
  29. interface ObServer 
  30. { 
  31.   public function update($event_info = null); 
  32. } 
  33.  
  34. /** 
  35.  * 观察者1 
  36.  */ 
  37. class ObServer1 implements ObServer 
  38. { 
  39.   public function update($event_info = null) 
  40.   { 
  41.     echo "观察者1 收到执行通知 执行完毕!\n"; 
  42.   } 
  43. } 
  44.  
  45. /** 
  46.  * 观察者1 
  47.  */ 
  48. class ObServer2 implements ObServer 
  49. { 
  50.   public function update($event_info = null) 
  51.   { 
  52.     echo "观察者2 收到执行通知 执行完毕!\n"; 
  53.   } 
  54. } 
  55.  
  56. /** 
  57.  * 事件 
  58.  * Class Event 
  59.  */ 
  60. class Event extends EventGenerator 
  61. { 
  62.   /** 
  63.    * 触发事件 
  64.    */ 
  65.   public function trigger() 
  66.   { 
  67.     //通知观察者 
  68.     $this->notify(); 
  69.   } 
  70. } 
  71.  
  72. //创建一个事件 
  73. $event = new Event(); 
  74. //为事件增加旁观者 
  75. $event->add(new ObServer1()); 
  76. $event->add(new ObServer2()); 
  77. //执行事件 通知旁观者 
  78. $event->trigger(); 

Tags: PHP设计模式

分享到: