PHP设计模式之:外观模式

外观模式:

外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用;

外观模式又称为门面模式,它是一种对象结构型模式。

模式结构:

 

外观模式就是让client客户端以一种简单的方式来调用比较复杂的系统来完成一件事情;

目的:

1、为一个复杂子系统提供简单的接口

2、减少客户端和子系统的耦合

代码实现:

示例1

 

  1  /**
  2 
  3  * 外观模式
  4 
  5  * 通过在必须的逻辑和方法的集合前创建简单的外观接口,外观设计模式隐藏了来自调用对象的复杂性
  6 
  7  */
  8 
  9  
 10 
 11 /1**
 12 
 13  * 
 14 
 15  * User类
 16 
 17  * @author lzs
 18 
 19  *
 20 
 21  */
 22 
 23 class User
 24 
 25 {
 26 
 27 protected $userName;
 28 
 29 protected $userAge;
 30 
 31  
 32 
 33 public function setUserName($userName)
 34 
 35 {
 36 
 37 return $this->userName = $userName;
 38 
 39 }
 40 
 41  
 42 
 43 public function setUserAge($userAge)
 44 
 45 {
 46 
 47 return $this->userAge = $userAge;
 48 
 49 }
 50 
 51  
 52 
 53 /1**
 54 
 55  * getUser方法
 56 
 57  * 获取用户信息
 58 
 59  */
 60 
 61 public function getUserInfo()
 62 
 63 {
 64 
 65 echo '用户姓名:'.$this->userName.'<br />用户年龄:'.$this->userAge;
 66 
 67 }
 68 
 69 }
 70 
 71  
 72 
 73 /1**
 74 
 75  * UserFacade外观模式类,简化获取用户getUserInfo方法的调用
 76 
 77  */
 78 
 79 class UserFacade
 80 
 81 {
 82 
 83 public static function getUser($userInfo)
 84 
 85 {
 86 
 87 $User = new User();
 88 
 89 $User->setUserName($userInfo['username']);
 90 
 91 $User->setUserAge($userInfo['userAge']);
 92 
 93 return $User->getUserInfo();
 94 
 95 }
 96 
 97 }
 98 
 99  
100 
101 $userInfo = array(
102 
103 'username' => 'lzs',
104 
105 'userAge'  => 21,
106 
107 );
108 
109 //简化调用类
110 
111 UserFacade::getUser($userInfo);
View Code

 

示例2

 

  1  /**
  2 
  3  * 外观模式
  4 
  5  */
  6 
  7  
  8 
  9 /1**
 10 
 11  * 创建多个子系统类
 12 
 13  */ 
 14 
 15  
 16 
 17 class SubSystem1
 18 
 19 {
 20 
 21 public function method1()
 22 
 23 {
 24 
 25 echo ' subsystem1 method1<br />';
 26 
 27 }
 28 
 29 }
 30 
 31  
 32 
 33 class SubSystem2
 34 
 35 {
 36 
 37 public function method2()
 38 
 39 {
 40 
 41 echo ' subsystem2 method2<br />';
 42 
 43 }
 44 
 45 }
 46 
 47  
 48 
 49 class SubSystem3
 50 
 51 {
 52 
 53 public function method3()
 54 
 55 {
 56 
 57 echo ' subsystem3 method3<br />';
 58 
 59 }
 60 
 61 }
 62 
 63  
 64 
 65 /1**
 66 
 67  * 与子系统通信的外观类,
 68 
 69  * 外部与一个子系统的通信必须通过一个统一的外观对象进行
 70 
 71  */
 72 
 73 class Facade
 74 
 75 {
 76 
 77 private $object1 = null;
 78 
 79 private $object2 = null;
 80 
 81 private $object3 = null;
 82 
 83  
 84 
 85 public function __construct()
 86 
 87 {
 88 
 89 $this->object1 = new SubSystem1();
 90 
 91 $this->object2 = new SubSystem2();
 92 
 93 $this->object3 = new SubSystem3();
 94 
 95 }
 96 
 97  
 98 
 99 public function methodFirst()
100 
101 {
102 
103 echo 'Facade methodFirst<br />';
104 
105 $this->object1->method1();
106 
107 $this->object2->method2();
108 
109 }
110 
111  
112 
113 public function methodSecond()
114 
115 {
116 
117 echo 'Facade methodSecond<br />';
118 
119 $this->object2->method2();
120 
121 $this->object3->method3();
122 
123 }
124 
125 }
126 
127  
128 
129 $objFacade = new Facade();
130 
131 $objFacade->methodFirst();
132 
133 $objFacade->methodSecond(); 
View Code

 

 

 

 

posted @ 2013-12-20 14:41  Otgs  阅读(250)  评论(0)    收藏  举报