Php设计模式:行为型模式(二)_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > Php设计模式:行为型模式(二)

Php设计模式:行为型模式(二)

 2014/5/19 23:09:43  cyber4cn  程序员俱乐部  我要评论(0)
  • 摘要:原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=12可以在线运行查看效果哦!<接上一篇>4、观察者模式(Observer):又叫发布订阅模式,当一个主体对象发生改变时,依赖它的多个观察者对象都得到通知并自动更新响应。就像报社一样,今天发布的消息只要是看这份报纸的人看到的都是同样的内容。如果发布另一份报纸,也是一样的。好处:广播式通信,范围大,一呼百应,便于操作一个组团,“公有制”。弊端:不能单独操作组团里的个体
  • 标签:PHP 模式 设计 设计模式

原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=12

可以在线运行查看效果哦! ? ?

?

<接上一篇>

4、观察者模式(Observer):

? ? ? ? ?又叫发布订阅模式,当一个主体对象发生改变时,依赖它的多个观察者对象都得到通知并自动更新响应。就像报社一样,今天发布的消息只要是看这份报纸的人看到的都是同样的内容。如果发布另一份报纸,也是一样的。

???????? 好处:广播式通信,范围大,一呼百应,便于操作一个组团,“公有制”。

???????? 弊端:不能单独操作组团里的个体,不能实行按需分配。

? ? ? ? ?应用场景:操作多个对象,并操作相同。

代码实现:

?

?

[php]?view plaincopy在CODE上查看代码片派生到我的代码片 ?
    class="dp-c">
  1. <?php??
  2. ??
  3. /**?
  4. ?*?优才网公开课示例代码?
  5. ?*?
  6. ?*?观察者模式?Observer?
  7. ?*?
  8. ?*?@author?优才网全栈工程师教研组?
  9. ?*?@see?http://www.ucai.cn?
  10. ?*/??
  11. ??
  12. function?output($string)?{??
  13. ????echo????$string?.?"\n";??
  14. }??
  15. ??
  16. ??
  17. //订单数据对象简单模拟,这个是实际需要被观察的对象(Subject),但是我们将其独立,然后??
  18. //通过构造方法传入到我们模式中的Subject中,这样使具体业务更加独立??
  19. class?Order{??
  20. ????//订单号??
  21. ????private?$id?=?'';??
  22. ??
  23. ????//用户ID??
  24. ????private?$userId?=?'';??
  25. ??
  26. ????//用户名??
  27. ????private?$userName?=?'';??
  28. ??
  29. ????//价格??
  30. ????private?$price?=?'';??
  31. ??
  32. ????//下单时间??
  33. ????private?$orderTime?=?'';??
  34. ??
  35. ????//订单数据填充简单模拟,实际应用中可能会读取用户表单输入并处理??
  36. ????public?function?__set($name,?$value){??
  37. ????????if?(isset($this->$name)){??
  38. ????????????$this->$name?=?$value;??
  39. ????????}??
  40. ????}??
  41. ??
  42. ????//获取订单属性??
  43. ????public?function?__get($name){??
  44. ????????if?(isset($this->$name)){??
  45. ????????????return?$this->$name;??
  46. ????????}??
  47. ????????return?"";??
  48. ????}??
  49. }??
  50. ??
  51. //假设的DB类,便于测试,实际会存入真实数据库??
  52. class?FakeDB{??
  53. ????public?function?save($data){??
  54. ????????return?true;??
  55. ????}??
  56. }??
  57. ??
  58. ??
  59. class?Client?{??
  60. ??????
  61. ????public?static?function?test()?{??
  62. ??
  63. ????????//初始化一个订单数据??
  64. ????????$order?=?new?Order();??
  65. ????????$order->id?=?1001;??
  66. ????????$order->userId?=?9527;??
  67. ????????$order->userName?=?"God";??
  68. ????????$order->price?=?20.0;??
  69. ????????$order->orderTime?=?time();??
  70. ??
  71. ????????//向数据库保存订单??
  72. ????????$db?=?new?FakeDB();??
  73. ????????$result?=?$db->save($order);??
  74. ????????if?($result){??
  75. ??
  76. ????????????//实际应用可能会写到日志文件中,这里直接输出??
  77. ????????????output(?"[OrderId:{$order->id}]?[UseId:{$order->userId}]?[Price:{$order->price}]"?);??
  78. ??
  79. ????????????//实际应用会调用邮件发送服务如sendmail,这里直接输出??
  80. ????????????output(?"Dear?{$order->userName}:?Your?order?{$order->id}?was?confirmed!"?);??
  81. ??
  82. ????????????//实际应用会调用邮件发送服务如sendmail,这里直接输出??
  83. ????????????output(?"Dear?Manager:?User?{$order->userName}(ID:{$order->userId})?submitted?a?new?order?{$order->id},?please?handle?it?ASAP!"?);??
  84. ??
  85. ????????}??
  86. ??
  87. ????}??
  88. ??
  89. }??
  90. ??
  91. Client::test();??
  92. ??
  93. ??
  94. <?php??
  95. ??
  96. /**?
  97. ?*?优才网公开课示例代码?
  98. ?*?
  99. ?*?观察者模式?Observer?
  100. ?*?
  101. ?*?@author?优才网全栈工程师教研组?
  102. ?*?@see?http://www.ucai.cn?
  103. ?*/??
  104. ??
  105. function?output($string)?{??
  106. ????echo????$string?.?"\n";??
  107. }??
  108. ??
  109. ??
  110. //订单数据对象简单模拟,这个是实际需要被观察的对象(Subject),但是我们将其独立,然后??
  111. //通过构造方法传入到我们模式中的Subject中,这样使具体业务更加独立??
  112. class?Order{??
  113. ????//订单号??
  114. ????private?$id?=?'';??
  115. ??
  116. ????//用户ID??
  117. ????private?$userId?=?'';??
  118. ??
  119. ????//用户名??
  120. ????private?$userName?=?'';??
  121. ??
  122. ????//价格??
  123. ????private?$price?=?'';??
  124. ??
  125. ????//下单时间??
  126. ????private?$orderTime?=?'';??
  127. ??
  128. ????//订单数据填充简单模拟,实际应用中可能会读取用户表单输入并处理??
  129. ????public?function?__set($name,?$value){??
  130. ????????if?(isset($this->$name)){??
  131. ????????????$this->$name?=?$value;??
  132. ????????}??
  133. ????}??
  134. ??
  135. ????//获取订单属性??
  136. ????public?function?__get($name){??
  137. ????????if?(isset($this->$name)){??
  138. ????????????return?$this->$name;??
  139. ????????}??
  140. ????????return?"";??
  141. ????}??
  142. }??
  143. ??
  144. //被观察者,?负责维护观察者并在变化发生是通知观察者??
  145. class?OrderSubject?implements?SplSubject?{??
  146. ????private?$observers;??
  147. ????private?$order;??
  148. ??
  149. ????public?function?__construct(Order?$order)?{??
  150. ????????$this->observers?=?new?SplObjectStorage();??
  151. ????????$this->order?=?$order;??
  152. ????}??
  153. ??
  154. ????//增加一个观察者??
  155. ????public?function?attach(SplObserver?$observer)?{??
  156. ????????$this->observers->attach($observer);??
  157. ????}??
  158. ??
  159. ????//移除一个观察者??
  160. ????public?function?detach(SplObserver?$observer)?{??
  161. ????????$this->observers->detach($observer);??
  162. ????}??
  163. ??
  164. ????//通知所有观察者??
  165. ????public?function?notify()?{??
  166. ????????foreach?($this->observers?as?$observer)?{??
  167. ????????????$observer->update($this);??
  168. ????????}??
  169. ????}??
  170. ??
  171. ????//返回主体对象的具体实现,供观察者调用??
  172. ????public?function?getOrder()?{??
  173. ????????return?$this->order;??
  174. ????}??
  175. }??
  176. ??
  177. //记录业务数据日志?(ActionLogObserver),实际可能还要抽象一层以处理不同的Action(业务操作),这里省略??
  178. class?ActionLogObserver?implements?SplObserver{??
  179. ????public?function?update(SplSubject?$subject)?{??
  180. ?????????$order?=?$subject->getOrder();??
  181. ?????????//实际应用可能会写到日志文件中,这里直接输出??
  182. ?????????output(?"[OrderId:{$order->id}]?[UseId:{$order->userId}]?[Price:{$order->price}]"?);??
  183. ????}??
  184. }??
  185. ??
  186. //给用户发送订单确认邮件?(UserMailObserver)??
  187. class?UserMailObserver?implements?SplObserver{??
  188. ????public?function?update(SplSubject?$subject)?{??
  189. ?????????$order?=?$subject->getOrder();??
  190. ?????????//实际应用会调用邮件发送服务如sendmail,这里直接输出??
  191. ?????????output(?"Dear?{$order->userName}:?Your?order?{$order->id}?was?confirmed!"?);??
  192. ????}??
  193. }??
  194. ??
  195. //给管理人员发订单处理通知邮件?(AdminMailObserver)??
  196. class?AdminMailObserver?implements?SplObserver{??
  197. ????public?function?update(SplSubject?$subject)?{??
  198. ?????????$order?=?$subject->getOrder();??
  199. ?????????//实际应用会调用邮件发送服务如sendmail,这里直接输出??
  200. ?????????output(?"Dear?Manager:?User?{$order->userName}(ID:{$order->userId})?submitted?a?new?order?{$order->id},?please?handle?it?ASAP!"?);??
  201. ????}??
  202. }??
  203. ??
  204. //假设的DB类,便于测试,实际会存入真实数据库??
  205. class?FakeDB{??
  206. ????public?function?save($data){??
  207. ????????return?true;??
  208. ????}??
  209. }??
  210. ??
  211. ??
  212. class?Client?{??
  213. ??????
  214. ????public?static?function?test()?{??
  215. ??
  216. ????????//初始化一个订单数据??
  217. ????????$order?=?new?Order();??
  218. ????????$order->id?=?1001;??
  219. ????????$order->userId?=?9527;??
  220. ????????$order->userName?=?"God";??
  221. ????????$order->price?=?20.0;??
  222. ????????$order->orderTime?=?time();??
  223. ??
  224. ????????//绑定观察者??
  225. ????????$subject?=?new?OrderSubject($order);??
  226. ????????$actionLogObserver?=?new?ActionLogObserver();??
  227. ????????$userMailObserver?=?new?UserMailObserver();??
  228. ????????$adminMailObserver?=?new?AdminMailObserver();??
  229. ????????$subject->attach($actionLogObserver);??
  230. ????????$subject->attach($userMailObserver);??
  231. ????????$subject->attach($adminMailObserver);??
  232. ????????//向数据库保存订单??
  233. ????????$db?=?new?FakeDB();??
  234. ????????$result?=?$db->save($order);??
  235. ????????if?($result){??
  236. ????????????//通知观察者??
  237. ????????????$subject->notify();??
  238. ????????}??
  239. ??
  240. ????}??
  241. ??
  242. }??
  243. ??
  244. Client::test();??

?

?

5、中介者模式(Mediator):

? ? ? ? ?用中介对象封装一系列的对象交互,中介使各对象不需要显式地相互引用。类似于邮局,邮寄者和收件者不用自己跑很远路,通过邮局就可以。

? ? ? ? ?好处:简化了对象之间的关系,减少子类的生成。

? ? ? ? ?弊端:中介对象可能变得非常复杂,系统难以维护。

? ? ? ? ?应用场景:不需要显示地建立交互。

代码实现:

?

[php]?view plaincopy在CODE上查看代码片派生到我的代码片 ?
  1. <?php??
  2. ??
  3. /**?
  4. ?*?优才网公开课示例代码?
  5. ?*?
  6. ?*?中介者模式?Mediator?
  7. ?*?
  8. ?*?@author?优才网全栈工程师教研组?
  9. ?*?@see?http://www.ucai.cn?
  10. ?*/??
  11. ??
  12. ??
  13. function?output($string)?{??
  14. ????echo????$string?.?"\n";??
  15. }??
  16. ??
  17. ??
  18. ??
  19. ??
  20. abstract?class?Mediator?{?//?中介者角色??
  21. ????abstract?public?function?send($message,$colleague);???
  22. }???
  23. ??
  24. abstract?class?Colleague?{?//?抽象对象??
  25. ????private?$_mediator?=?null;???
  26. ????public?function?__construct($mediator)?{???
  27. ????????$this->_mediator?=?$mediator;???
  28. ????}???
  29. ????public?function?send($message)?{???
  30. ????????$this->_mediator->send($message,$this);???
  31. ????}???
  32. ????abstract?public?function?notify($message);???
  33. }???
  34. ??
  35. class?ConcreteMediator?extends?Mediator?{?//?具体中介者角色??
  36. ????private?$_colleague1?=?null;???
  37. ????private?$_colleague2?=?null;???
  38. ????public?function?send($message,$colleague)?{???
  39. ????????if($colleague?==?$this->_colleague1)?{???
  40. ????????????$this->_colleague1->notify($message);???
  41. ????????}?else?{???
  42. ????????????$this->_colleague2->notify($message);???
  43. ????????}???
  44. ????}??
  45. ????public?function?set($colleague1,$colleague2)?{???
  46. ????????$this->_colleague1?=?$colleague1;???
  47. ????????$this->_colleague2?=?$colleague2;???
  48. ????}???
  49. }???
  50. ??
  51. class?Colleague1?extends?Colleague?{?//?具体对象角色??
  52. ????public?function?notify($message)?{??
  53. ????????output(sprintf('Colleague-1:?%s',?$message));??
  54. ????}???
  55. }???
  56. ??
  57. class?Colleague2?extends?Colleague?{?//?具体对象角色??
  58. ????public?function?notify($message)?{???
  59. ????????output(sprintf('Colleague-2:?%s',?$message));??
  60. ????}???
  61. }???
  62. ??
  63. ??
  64. ??
  65. class?Client?{????
  66. ????????
  67. ????public?static?function?test(){????
  68. ??
  69. ????????//?client??
  70. ????????$objMediator?=?new?ConcreteMediator();???
  71. ????????$objC1?=?new?Colleague1($objMediator);???
  72. ????????$objC2?=?new?Colleague2($objMediator);???
  73. ????????$objMediator->set($objC1,$objC2);???
  74. ????????$objC1->send("to?c2?from?c1");???
  75. ????????$objC2->send("to?c1?from?c2");???
  76. ??
  77. ????}????
  78. ????????
  79. }????
  80. ????
  81. Client::test();???

?

?

6、状态模式(State) :

? ? ? ? ? 对象在不同状态下表现出不同的行为。就像女朋友一样,高兴了牵你的手,不高兴了遛狗。在两种状态下变现出不同的行为。

? ? ? ? ?好处:避免if语句实用,方便增加新状态,封装了状态转换规则。

? ? ? ? ?弊端:增加系统类和对象的数量。

? ? ? ? ?应用场景:用于对象的不同功能的转换。

代码实现:

?

[php]?view plaincopy在CODE上查看代码片派生到我的代码片 ?
  1. <?php??
  2. ??
  3. /**?
  4. ?*?优才网公开课示例代码?
  5. ?*?
  6. ?*?状态模式?State?
  7. ?*?
  8. ?*?@author?优才网全栈工程师教研组?
  9. ?*?@see?http://www.ucai.cn?
  10. ?*/??
  11. ??
  12. function?output($string)?{??
  13. ????echo????$string?.?"\n";??
  14. }??
  15. ??
  16. abstract?class?ILift?{????
  17. ??
  18. ????//电梯的四个状态????
  19. ????const?OPENING_STATE?=?1;??//门敞状态????
  20. ????const?CLOSING_STATE?=?2;??//门闭状态????
  21. ????const?RUNNING_STATE?=?3;??//运行状态????
  22. ????const?STOPPING_STATE?=?4;?//停止状态;????
  23. ????????
  24. ????//设置电梯的状态????
  25. ????public?abstract?function?setState($state);????
  26. ????
  27. ????//首先电梯门开启动作????
  28. ????public?abstract?function?open();????
  29. ????
  30. ????//电梯门有开启,那当然也就有关闭了????
  31. ????public?abstract?function?close();????
  32. ????
  33. ????//电梯要能上能下,跑起来????
  34. ????public?abstract?function?run();????
  35. ????
  36. ????//电梯还要能停下来??
  37. ????public?abstract?function?stop();????
  38. ??
  39. }????
  40. ????
  41. /**??
  42. ?*?电梯的实现类???
  43. ?*/?????
  44. class?Lift?extends?ILift?{????
  45. ??
  46. ????private?$state;????
  47. ????
  48. ????public?function?setState($state)?{????
  49. ????????$this->state?=?$state;????
  50. ????}????
  51. ??
  52. ????//电梯门关闭????
  53. ????public?function?close()?{????
  54. ??
  55. ????????//电梯在什么状态下才能关闭????
  56. ????????switch?($this->state)?{????
  57. ????????????case?ILift::OPENING_STATE:??//如果是则可以关门,同时修改电梯状态????
  58. ????????????????$this->setState(ILift::CLOSING_STATE);????
  59. ????????????break;????
  60. ????????????case?ILift::CLOSING_STATE:??//如果电梯就是关门状态,则什么都不做????
  61. ????????????????//do?nothing;????
  62. ????????????????return?;????
  63. ????????????break;????
  64. ????????????case?ILift::RUNNING_STATE:?//如果是正在运行,门本来就是关闭的,也说明都不做????
  65. ????????????????//do?nothing;????
  66. ????????????????return?;????
  67. ????????????break;????
  68. ????????????case?ILift::STOPPING_STATE:??//如果是停止状态,本也是关闭的,什么也不做????
  69. ????????????????//do?nothing;????
  70. ????????????????return?;????
  71. ????????????break;????
  72. ????????}????
  73. ??
  74. ????????output('Lift?colse');????
  75. ??
  76. ????}????
  77. ????
  78. ????//电梯门开启????
  79. ????public?function?open()?{????
  80. ????????//电梯在什么状态才能开启????
  81. ????????switch($this->state){????
  82. ????????????case?ILift::OPENING_STATE:?//如果已经在门敞状态,则什么都不做????
  83. ????????????????//do?nothing;????
  84. ????????????????return?;????
  85. ????????????break;????
  86. ????????????case?ILift::CLOSING_STATE:?//如是电梯时关闭状态,则可以开启????
  87. ????????????????$this->setState(ILift::OPENING_STATE);????
  88. ????????????break;????
  89. ????????????case?ILift::RUNNING_STATE:?//正在运行状态,则不能开门,什么都不做????
  90. ????????????//do?nothing;????
  91. ????????????????return?;????
  92. ????????????break;????
  93. ????????????case?ILift::STOPPING_STATE:?//停止状态,淡然要开门了????
  94. ????????????????$this->setState(ILift::OPENING_STATE);????
  95. ????????????break;????
  96. ????????}????
  97. ????????output('Lift?open');????
  98. ????}????
  99. ????///电梯开始跑起来????
  100. ????public?function?run()?{????
  101. ????????switch($this->state){????
  102. ????????????case?ILift::OPENING_STATE:?//如果已经在门敞状态,则不你能运行,什么都不做????
  103. ????????????????//do?nothing;????
  104. ????????????????return?;????
  105. ????????????break;????
  106. ????????????case?ILift::CLOSING_STATE:?//如是电梯时关闭状态,则可以运行????
  107. ????????????????$this->setState(ILift::RUNNING_STATE);????
  108. ????????????break;????
  109. ????????????case?ILift::RUNNING_STATE:?//正在运行状态,则什么都不做????
  110. ????????????????//do?nothing;????
  111. ????????????????return?;????
  112. ????????????break;????
  113. ????????????case?ILift::STOPPING_STATE:?//停止状态,可以运行????
  114. ????????????????$this->setState(ILift::RUNNING_STATE);????
  115. ????????}????
  116. ????????output('Lift?run');????
  117. ????}????
  118. ????
  119. ????//电梯停止????
  120. ????public?function?stop()?{????
  121. ????????switch($this->state){????
  122. ????????????case?ILift::OPENING_STATE:?//如果已经在门敞状态,那肯定要先停下来的,什么都不做????
  123. ????????????????//do?nothing;????
  124. ????????????????return?;????
  125. ????????????break;????
  126. ????????????case?ILift::CLOSING_STATE:?//如是电梯时关闭状态,则当然可以停止了????
  127. ????????????????$this->setState(ILift::CLOSING_STATE);????
  128. ????????????break;????
  129. ????????????case?ILift::RUNNING_STATE:?//正在运行状态,有运行当然那也就有停止了????
  130. ????????????????$this->setState(ILift::CLOSING_STATE);????
  131. ????????????break;????
  132. ????????????case?ILift::STOPPING_STATE:?//停止状态,什么都不做????
  133. ????????????????//do?nothing;????
  134. ????????????????return?;????
  135. ????????????break;????
  136. ????????}????
  137. ????????output('Lift?stop');????
  138. ????}????
  139. ????????
  140. }????
  141. ??
  142. ??
  143. ??
  144. class?Client?{??
  145. ??????
  146. ????public?static?function?test()?{??
  147. ??
  148. ????????$lift?=?new?Lift();?????
  149. ???????????????
  150. ????????//电梯的初始条件应该是停止状态?????
  151. ????????$lift->setState(ILift::STOPPING_STATE);?????
  152. ????????//首先是电梯门开启,人进去?????
  153. ????????$lift->open();?????
  154. ???????????????
  155. ????????//然后电梯门关闭?????
  156. ????????$lift->close();?????
  157. ???????????????
  158. ????????//再然后,电梯跑起来,向上或者向下?????
  159. ????????$lift->run();????????
  160. ??
  161. ?????????//最后到达目的地,电梯挺下来?????
  162. ????????$lift->stop();????
  163. ??
  164. ????}??
  165. ??
  166. }??
  167. ??
  168. Client::test();??
  169. ??
  170. ??
  171. <?php??
  172. ??
  173. /**?
  174. ?*?优才网公开课示例代码?
  175. ?*?
  176. ?*?状态模式?State?
  177. ?*?
  178. ?*?@author?优才网全栈工程师教研组?
  179. ?*?@see?http://www.ucai.cn?
  180. ?*/??
  181. ??
  182. function?output($string)?{??
  183. ????echo????$string?.?"\n";??
  184. }??
  185. ??
  186. /**??
  187. ?*???
  188. ?*?定义一个电梯的接口???
  189. ?*/?????
  190. abstract?class?LiftState{????
  191. ????
  192. ????//定义一个环境角色,也就是封装状态的变换引起的功能变化????
  193. ????protected??$_context;????
  194. ????
  195. ????public?function?setContext(Context?$context){????
  196. ????????$this->_context?=?$context;????
  197. ????}????
  198. ????
  199. ????//首先电梯门开启动作????
  200. ????public?abstract?function?open();????
  201. ????
  202. ????//电梯门有开启,那当然也就有关闭了????
  203. ????public?abstract?function?close();????
  204. ????
  205. ????//电梯要能上能下,跑起来????
  206. ????public?abstract?function?run();????
  207. ????
  208. ????//电梯还要能停下来,停不下来那就扯淡了????
  209. ????public?abstract?function?stop();????
  210. ????
  211. }????
  212. ????
  213. ????
  214. /**??
  215. ?*?环境类:定义客户感兴趣的接口。维护一个ConcreteState子类的实例,这个实例定义当前状态。??
  216. ?*/?????
  217. class?Context?{????
  218. ????//定义出所有的电梯状态????
  219. ????static??$openningState?=?null;????
  220. ????static??$closeingState?=?null;????
  221. ????static??$runningState??=?null;????
  222. ????static??$stoppingState?=?null;????
  223. ????
  224. ????public?function?__construct()?{????
  225. ????????self::$openningState?=?new?OpenningState();????
  226. ????????self::$closeingState?=?new?ClosingState();????
  227. ????????self::$runningState?=??new?RunningState();????
  228. ????????self::$stoppingState?=?new?StoppingState();????
  229. ????
  230. ????}????
  231. ????
  232. ????//定一个当前电梯状态????
  233. ????private??$_liftState;????
  234. ????
  235. ????public?function?getLiftState()?{????
  236. ????????return?$this->_liftState;????
  237. ????}????
  238. ????
  239. ????public?function?setLiftState($liftState)?{????
  240. ????????$this->_liftState?=?$liftState;????
  241. ????????//把当前的环境通知到各个实现类中????
  242. ????????$this->_liftState->setContext($this);????
  243. ????}????
  244. ????
  245. ????
  246. ????public?function?open(){????
  247. ????????$this->_liftState->open();????
  248. ????}????
  249. ????
  250. ????public?function?close(){????
  251. ????????$this->_liftState->close();????
  252. ????}????
  253. ????
  254. ????public?function?run(){????
  255. ????????$this->_liftState->run();????
  256. ????}????
  257. ????
  258. ????public?function?stop(){????
  259. ????????$this->_liftState->stop();????
  260. ????}????
  261. }????
  262. ????
  263. /**??
  264. ?*?在电梯门开启的状态下能做什么事情???
  265. ?*/?????
  266. class?OpenningState?extends?LiftState?{????
  267. ????
  268. ????/**??
  269. ?????*?开启当然可以关闭了,我就想测试一下电梯门开关功能??
  270. ?????*??
  271. ?????*/????
  272. ????public?function?close()?{????
  273. ????????//状态修改????
  274. ????????$this->_context->setLiftState(Context::$closeingState);????
  275. ????????//动作委托为CloseState来执行????
  276. ????????$this->_context->getLiftState()->close();????
  277. ????}????
  278. ????
  279. ????//打开电梯门????
  280. ????public?function?open()?{????
  281. ????????output('lift?open...');??
  282. ????}????
  283. ????//门开着电梯就想跑,这电梯,吓死你!????
  284. ????public?function?run()?{????
  285. ????????//do?nothing;????
  286. ????}????
  287. ????
  288. ????//开门还不停止?????
  289. ????public?function?stop()?{????
  290. ????????//do?nothing;????
  291. ????}????
  292. ????
  293. }????
  294. ????
  295. /**??
  296. ?*?电梯门关闭以后,电梯可以做哪些事情???
  297. ?*/?????
  298. class?ClosingState?extends?LiftState?{????
  299. ????
  300. ????//电梯门关闭,这是关闭状态要实现的动作????
  301. ????public?function?close()?{????
  302. ????????output('lift?close...');??
  303. ????
  304. ????}????
  305. ????//电梯门关了再打开,逗你玩呢,那这个允许呀????
  306. ????public?function?open()?{????
  307. ????????$this->_context->setLiftState(Context::$openningState);??//置为门敞状态????
  308. ????????$this->_context->getLiftState()->open();????
  309. ????}????
  310. ????
  311. ????//电梯门关了就跑,这是再正常不过了????
  312. ????public?function?run()?{????
  313. ????????$this->_context->setLiftState(Context::$runningState);?//设置为运行状态;????
  314. ????????$this->_context->getLiftState()->run();????
  315. ????}????
  316. ????
  317. ????//电梯门关着,我就不按楼层????
  318. ????????
  319. ????public?function?stop()?{????
  320. ????????$this->_context->setLiftState(Context::$stoppingState);??//设置为停止状态;????
  321. ????????$this->_context->getLiftState()->stop();????
  322. ????}????
  323. ????
  324. }????
  325. ????
  326. /**??
  327. ?*?电梯在运行状态下能做哪些动作???
  328. ?*/?????
  329. class?RunningState?extends?LiftState?{????
  330. ????
  331. ????//电梯门关闭?这是肯定了????
  332. ????public?function?close()?{????
  333. ????????//do?nothing????
  334. ????}????
  335. ????
  336. ????//运行的时候开电梯门?你疯了!电梯不会给你开的????
  337. ????public?function?open()?{????
  338. ????????//do?nothing????
  339. ????}????
  340. ????
  341. ????//这是在运行状态下要实现的方法????
  342. ????public?function?run()?{????
  343. ????????output('lift?run...');??
  344. ????}????
  345. ????
  346. ????//这个事绝对是合理的,光运行不停止还有谁敢做这个电梯?!估计只有上帝了????
  347. ????public?function?stop()?{????
  348. ????????$this->_context->setLiftState(Context::$stoppingState);?//环境设置为停止状态;????
  349. ????????$this->_context->getLiftState()->stop();????
  350. ????}????
  351. ????
  352. }????
  353. ????
  354. ????
  355. ????
  356. /**??
  357. ?*?在停止状态下能做什么事情???
  358. ?*/?????
  359. class?StoppingState?extends?LiftState?{????
  360. ????
  361. ????//停止状态关门?电梯门本来就是关着的!????
  362. ????public?function?close()?{????
  363. ????????//do?nothing;????
  364. ????}????
  365. ????
  366. ????//停止状态,开门,那是要的!????
  367. ????public?function?open()?{????
  368. ????????$this->_context->setLiftState(Context::$openningState);????
  369. ????????$this->_context->getLiftState()->open();????
  370. ????}????
  371. ????//停止状态再跑起来,正常的很????
  372. ????public?function?run()?{????
  373. ????????$this->_context->setLiftState(Context::$runningState);????
  374. ????????$this->_context->getLiftState()->run();????
  375. ????}????
  376. ????//停止状态是怎么发生的呢?当然是停止方法执行了????
  377. ????public?function?stop()?{????
  378. ????????output('lift?stop...');??
  379. ????}????
  380. ????
  381. }????
  382. ????
  383. /**??
  384. ?*?模拟电梯的动作???
  385. ?*/?????
  386. class?Client?{????
  387. ????
  388. ????public?static?function?test()?{????
  389. ????????$context?=?new?Context();????
  390. ????????$context->setLiftState(new?ClosingState());????
  391. ????
  392. ????????$context->open();????
  393. ????????$context->close();????
  394. ????????$context->run();????
  395. ????????$context->stop();????
  396. ????}????
  397. }????
  398. ??
  399. Client::test(); ??
发表评论
用户名: 匿名