﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-老范记事-随笔分类-PHP</title><link>http://www.cnitblog.com/fanyh/category/8623.html</link><description>If you think you can, you can!</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 19:17:01 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 19:17:01 GMT</pubDate><ttl>60</ttl><item><title>Observer模式</title><link>http://www.cnitblog.com/fanyh/archive/2010/06/14/66752.html</link><dc:creator>fanyh</dc:creator><author>fanyh</author><pubDate>Mon, 14 Jun 2010 15:54:00 GMT</pubDate><guid>http://www.cnitblog.com/fanyh/archive/2010/06/14/66752.html</guid><wfw:comment>http://www.cnitblog.com/fanyh/comments/66752.html</wfw:comment><comments>http://www.cnitblog.com/fanyh/archive/2010/06/14/66752.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/fanyh/comments/commentRss/66752.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/fanyh/services/trackbacks/66752.html</trackback:ping><description><![CDATA[<p>UML图<br><img border=0 src="http://www.cnitblog.com/images/cnitblog_com/fanyh/Observer_class_diagram.png" width=819 height=604><br><br>代码：<br>&lt;?php<br>class Customer{<br>&nbsp;private $id ;<br>&nbsp;private $name ;<br>&nbsp;private $address ;<br>&nbsp;private $tel ;<br>&nbsp;private static $observers = array() ;<br>&nbsp;/**<br>&nbsp; * @return the $id<br>&nbsp; */<br>&nbsp;public function getId() {<br>&nbsp;&nbsp;return $this-&gt;id;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @return the $name<br>&nbsp; */<br>&nbsp;public function getName() {<br>&nbsp;&nbsp;return $this-&gt;name;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @return the $address<br>&nbsp; */<br>&nbsp;public function getAddress() {<br>&nbsp;&nbsp;return $this-&gt;address;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @return the $tel<br>&nbsp; */<br>&nbsp;public function getTel() {<br>&nbsp;&nbsp;return $this-&gt;tel;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @param $id the $id to set<br>&nbsp; */<br>&nbsp;public function setId($id) {<br>&nbsp;&nbsp;$this-&gt;id = $id;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @param $name the $name to set<br>&nbsp; */<br>&nbsp;public function setName($name) {<br>&nbsp;&nbsp;$this-&gt;name = $name;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @param $address the $address to set<br>&nbsp; */<br>&nbsp;public function setAddress($address) {<br>&nbsp;&nbsp;$this-&gt;address = $address;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * @param $tel the $tel to set<br>&nbsp; */<br>&nbsp;public function setTel($tel) {<br>&nbsp;&nbsp;$this-&gt;tel = $tel;<br>&nbsp;}<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; * @param Observer $observer<br>&nbsp; */<br>&nbsp;public static function attach($observer){<br>&nbsp;&nbsp;if (!in_array($observer , self::$observers)){<br>&nbsp;&nbsp;&nbsp;self::$observers[] = $observer ;<br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; * @param Observer $observer<br>&nbsp; */<br>&nbsp;public static function detach($observer){<br>&nbsp;&nbsp;foreach (self::$observers as $k =&gt; $item) {<br>&nbsp;&nbsp;&nbsp;if ($item == $observer){<br>&nbsp;&nbsp;&nbsp;&nbsp;unset(self::$observers[$k]) ;<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;/**<br>&nbsp; * 通知所有的观察者<br>&nbsp; */<br>&nbsp;public function notify(){<br>&nbsp;&nbsp;foreach (self::$observers as $observer) {<br>&nbsp;&nbsp;&nbsp;$observer-&gt;update($this) ;<br>&nbsp;&nbsp;}<br>&nbsp;}<br>}<br>interface Observer{<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; * @param Customer $customer<br>&nbsp; */<br>&nbsp;public function update($customer) ;<br>&nbsp;<br>}</p>
<p>class Address implements Observer{<br>&nbsp;private $address = 'tianhe' ;<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; * @param Customer $customer<br>&nbsp; */<br>&nbsp;public function update($customer){<br>&nbsp;&nbsp;if ($customer-&gt;getAddress() != $this-&gt;address){<br>&nbsp;&nbsp;&nbsp;echo '地址变了...................' ;<br>&nbsp;&nbsp;&nbsp;echo '&lt;br&gt;' ;<br>&nbsp;&nbsp;}<br>&nbsp;}<br>}</p>
<p>class Tel implements Observer{<br>&nbsp;private $tel = 13602413192 ;<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; * @param Customer $customer<br>&nbsp; */<br>&nbsp;public function update($customer){<br>&nbsp;&nbsp;if ($customer-&gt;getTel() != $this-&gt;tel){<br>&nbsp;&nbsp;&nbsp;echo '电话变了...............' ;<br>&nbsp;&nbsp;&nbsp;echo '&lt;br&gt;' ;<br>&nbsp;&nbsp;}<br>&nbsp;}<br>}<br>header("Content-Type:text/html;charset=utf-8") ;<br>$address = new Address() ;<br>$tel = new Tel() ;<br>Customer::attach($address) ;<br>Customer::attach($tel) ;</p>
<p>$customer = new Customer() ;<br>$customer-&gt;notify() ;</p>
<p>?&gt;<br><br>本模式特性:<br>1.被观察者不需要知道自己被那些对象(观察者)观察着,观察者知道自己要观察谁(调用被观察者的attach()方法)<br>2.被观察者只需要在属性发生改变时,调用notify()方法,则所有在观察自己的对象都知道做出何种反应(观察者通过update()来做出反应) </p>
<img src ="http://www.cnitblog.com/fanyh/aggbug/66752.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/fanyh/" target="_blank">fanyh</a> 2010-06-14 23:54 <a href="http://www.cnitblog.com/fanyh/archive/2010/06/14/66752.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP中的拦截器设计</title><link>http://www.cnitblog.com/fanyh/archive/2010/03/18/64720.html</link><dc:creator>fanyh</dc:creator><author>fanyh</author><pubDate>Thu, 18 Mar 2010 13:08:00 GMT</pubDate><guid>http://www.cnitblog.com/fanyh/archive/2010/03/18/64720.html</guid><wfw:comment>http://www.cnitblog.com/fanyh/comments/64720.html</wfw:comment><comments>http://www.cnitblog.com/fanyh/archive/2010/03/18/64720.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/fanyh/comments/commentRss/64720.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/fanyh/services/trackbacks/64720.html</trackback:ping><description><![CDATA[<p>&lt;?php<br>class Action{</p>
<p>&nbsp;public function perform(){<br>&nbsp;&nbsp;echo 'hello,fanyh!&lt;br&gt;' ;<br>&nbsp;}<br>}<br>/**<br>&nbsp;* Interceptor接口<br>&nbsp;* @author Administrator<br>&nbsp;*<br>&nbsp;*/<br>interface Interceptor{<br>&nbsp;/**<br>&nbsp; * 在指定的方法之前执行<br>&nbsp; */<br>&nbsp;public function doBefore() ;<br>&nbsp;/**<br>&nbsp; * 在指定的方法之后 执行<br>&nbsp; */<br>&nbsp;public function doAfter() ;<br>}<br>/**<br>&nbsp;* 所有Interceptor的基类<br>&nbsp;* @author Administrator<br>&nbsp;*<br>&nbsp;*/<br>abstract class AbstractInterceptor implements Interceptor{</p>
<p>&nbsp;public final function invoke($object,$method,$args=null){<br>&nbsp;&nbsp;$this-&gt;doBefore() ;<br>&nbsp;&nbsp;if(method_exists($object,$method)){<br>&nbsp;&nbsp;&nbsp;$object-&gt;$method($args);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;$this-&gt;doAfter() ;<br>&nbsp;}<br>}</p>
<p>/**<br>&nbsp;* 定义一个Interceptor<br>&nbsp;* @author Administrator<br>&nbsp;*<br>&nbsp;*/<br>class InterceptorImpl1 extends AbstractInterceptor{<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; */<br>&nbsp;public function doBefore() {<br>&nbsp;&nbsp;echo 'Before method......111111111111111111&lt;br&gt;' ;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * <br>&nbsp; */<br>&nbsp;public function doAfter() {<br>&nbsp;&nbsp;echo 'After method......1111111111111111111&lt;br&gt;' ;<br>&nbsp;}<br>}<br>/**<br>&nbsp;* 定义一个Interceptor<br>&nbsp;* @author Administrator<br>&nbsp;*<br>&nbsp;*/<br>class InterceptorImpl2 extends AbstractInterceptor{<br>&nbsp;/**<br>&nbsp; * <br>&nbsp; */<br>&nbsp;public function doBefore() {<br>&nbsp;&nbsp;echo 'Before method......2222222222222&lt;br&gt;' ;<br>&nbsp;}</p>
<p>&nbsp;/**<br>&nbsp; * <br>&nbsp; */<br>&nbsp;public function doAfter() {<br>&nbsp;&nbsp;echo 'After method......22222222222222222&lt;br&gt;' ;<br>&nbsp;}<br>}<br>/**<br>&nbsp;* 控制器类,同时作为Interceptor的容器<br>&nbsp;* @author Administrator<br>&nbsp;*<br>&nbsp;*/<br>class Controller{<br>&nbsp;private $interceptors = array();<br>&nbsp;private $index = 0 ;<br>&nbsp;/**<br>&nbsp; * 调用Interceptor中的方法来执行<br>&nbsp; */<br>&nbsp;public function invoke(){<br>&nbsp;&nbsp;if ($this-&gt;index&lt;count($this-&gt;interceptors)){<br>&nbsp;&nbsp;&nbsp;$this-&gt;interceptors[$this-&gt;index++]-&gt;invoke($this,'invoke') ;<br>&nbsp;&nbsp;}else{<br>&nbsp;&nbsp;&nbsp;$this-&gt;index = 0 ;<br>&nbsp;&nbsp;&nbsp;$action = new Action() ;<br>&nbsp;&nbsp;&nbsp;$action-&gt;perform() ;<br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;/**<br>&nbsp; * 增加Interceptor<br>&nbsp; * @param unknown_type $interceptor<br>&nbsp; */<br>&nbsp;public function addInterceptor($interceptor){<br>&nbsp;&nbsp;$this-&gt;interceptors[] = $interceptor ;<br>&nbsp;}<br>}</p>
<p><br>$controller = new Controller() ;</p>
<p>$controller-&gt;addInterceptor(new InterceptorImpl1()) ;<br>$controller-&gt;addInterceptor(new InterceptorImpl2()) ;<br>$controller-&gt;invoke() ;<br>?&gt;<br><br><span style="COLOR: red">代码运行结果:</span><br><br>Before method......111111111111111111<br>Before method......2222222222222<br>hello,fanyh!<br>After method......22222222222222222<br>After method......1111111111111111111<br><br>分析：<br>在实现MVC模式开发时，可以利用这种方式在action执行前对数据做一切处理，在经过action后再加处理<br>是不是有点java中的AOP的意思呢？<br></p>
<img src ="http://www.cnitblog.com/fanyh/aggbug/64720.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/fanyh/" target="_blank">fanyh</a> 2010-03-18 21:08 <a href="http://www.cnitblog.com/fanyh/archive/2010/03/18/64720.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>