让我们把注意力集中到中间服务层上来。中间服务层代码比较简单,只是调用数据访问层代码将留言保存到数据库。如代码1所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 1<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> }<br>};<br></span> </div>
在看到留言板的演示之后,公司的产品部和市场部或许会提出各种各样的想法和需求。比如他们希望在添加留言之前判断用户的权限!只有注册用户才能留言!我们需要修改代码,如代码2所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 2, 增加登录验证<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$userLogin</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 提示用户登录</span><span style="color: #008000;"><br></span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;"> }</span><br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> }<br>};<br></span> </div>
市场部又希望在添加留言之前,对留言内容进行检查,如果留言中含有脏话就不保存。我们继续修改代码,如代码3所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 3, 增加脏话过滤<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$userLogin</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 提示用户登录</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">stristr</span><span style="color: #000000;">(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">SB</span><span style="color: #000000;">"</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 含有脏话, 提示留言发送失败</span><span style="color: #008000;"><br></span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;"> }</span><br> <br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> }<br>};<br></span> </div>
产品部也提出了新需求,他们希望加入积分机制。具体来讲就是在用户每次留言成功以后给用户+5分。我们继续修改代码,如代码4所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 4, 加入留言积分机制<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$userLogin</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 提示用户登录</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">stristr</span><span style="color: #000000;">(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">SB</span><span style="color: #000000;">"</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 含有脏话, 提示留言发送失败</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 给用户加分</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> getUserScore(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">);<br> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">5</span><span style="color: #000000;">;<br> saveUserScore(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br> }<br>};<br></span> </div>
没过多久,产品部又对需求进行细化,他们希望用户积分每积累够1000分以后,就给用户升级。我们继续修改代码,如代码5所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 5, 加入用户升级规则<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$userLogin</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 提示用户登录</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">stristr</span><span style="color: #000000;">(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">fuck</span><span style="color: #000000;">"</span><span style="color: #000000;">)) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 含有脏话, 提示留言发送失败</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br> <br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 给用户加分</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> getUserScore(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">);<br> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">5</span><span style="color: #000000;">;<br> saveUserScore(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$score</span><span style="color: #000000;">);<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 给用户升级</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ((</span><span style="color: #800080;">$score</span><span style="color: #000000;"> </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #000000;">1000</span><span style="color: #000000;">) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$level</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> getUserLevel(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">);<br> </span><span style="color: #800080;">$level</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$level</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">;<br> saveUserLevel(</span><span style="color: #800080;">$userName</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$level</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">); <br> }</span><br> }<br>};<br></span> </div>
随着需求的增多,我们需要不断的修改中间服务层代码。但是你应该不难发现,需求越多中间服务层代码也就越多越庞大!最后会导致即便我们使用三层结构的开发模式,也还是没有有效的降低工程难度!另外就是应需求的变化而修改中间服务代码以后,需要重新测试所有代码,而不是有效的测试新增代码……
其实让我们仔细分析一下这个留言板代码,我先要提出一个主业务逻辑和次业务逻辑的概念。无论怎样,把留言内容存入到数据库,这是业务逻辑的主干!这个就是主业务逻辑!这部分没有随着需求的增加而修改。至于在存入数据库之前要进行权限校验,要进行内容检查,存入数据库之后要给用户加分,然后给用户升级,这些都是前序工作和扫尾工作,都是次业务逻辑!主业务逻辑几乎是一成不变的,次业务逻辑变化却非常频繁。为了提高代码的可读性和可维护性,我们可以考虑把这些次业务逻辑放到别的地方,尽量不要让它们干扰主业务逻辑。主业务逻辑专心干自己该干的事情好了,至于别的任何事情,主业务逻辑一概都不闻不问!那么我们的代码就可以写成这样,如代码6所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 6, 将主业务逻辑和次业务逻辑分开<br>// 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask();<br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br> }<br>};<br></span> </div>
我们可以把权限判断代码和留言内容文本过滤代码统统塞进beforeAppend函数,把用户积分代码塞进behindAppend函数,这样就把次业务逻辑从主业务逻辑代码中清理掉了。主业务逻辑知道有个“序曲”函数beforeAppend,有个“尾声”函数behindAppend,但是在序曲和尾声函数中具体都做了什么事情,主业务逻辑并不知道,也不需要知道!当然实际编码工作并不那么简单,我们还要兼顾产品部和市场部更多的需求变化,所以最好能实现一种插件方式来应对这种变化,但是仅仅依靠两个函数beforeAppend和behindAppend是达不到这个目的~
想要实现插件方式,可以建立接口!使用接口的好处是可以将定义和实现隔离,另外就是实现多态。我们建立一个留言扩展接口ILWordExtension,该接口有两个函数beforeAppend和behindAppend。权限校验、内容检查、加分这些功能可以看作是实现ILWordExtension接口的三个实现类,主业务逻辑就依次遍历这三个实现类,来完成次业务逻辑。如图1所示:
(图1),加入扩展接口
CheckPowerExtension扩展类用作用户权限校验,CheckContentExtension扩展类用作留言内容检查,AddScoreExtension扩展类用作给用户加分和升级。示意代码如代码7所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 7,加入扩展接口<br><span style="background-color: #e0e0e0;">// 扩展接口</span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;"><br></span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);<br>};</span><br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查权限</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> CheckPowerExtension </span><span style="text-decoration: underline;"><span style="color: #0000ff;">implements</span></span><span style="color: #000000;"><span style="text-decoration: underline;"> ILWordExtension</span> {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在这里判断用户权限</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查留言文本</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> CheckContentExtension </span><span style="text-decoration: underline;"><span style="color: #0000ff;">implements</span></span><span style="color: #000000;"><span style="text-decoration: underline;"> ILWordExtension</span> {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">stristr</span><span style="color: #000000;">(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">SB</span><span style="color: #000000;">"</span><span style="color: #000000;">)) {<br> </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">Exception</span><span style="color: #000000;">();<br> }<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 用户积分</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AddScoreExtension </span><span style="text-decoration: underline;"><span style="color: #0000ff;">implements</span></span><span style="color: #000000;"><span style="text-decoration: underline;"> ILWordExtension</span> {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在这里给用户积分</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">beforeAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask(); <br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取扩展数组</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$extArray</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">getExtArray();<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$extArray</span><span style="color: #000000;"> </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$ext</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历每一个扩展, 并调用其 beforeAppend 函数</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$ext</span><span style="color: #000000;">-></span><span style="color: #000000;">beforeAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);<br> }</span><br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取扩展数组</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$extArray</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">getExtArray();<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$extArray</span><span style="color: #000000;"> </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$ext</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历每一个扩展, 并调用其 behindAppend 函数</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$ext</span><span style="color: #000000;">-></span><span style="color: #000000;">behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);<br> }</span><br> }<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 获取扩展数组, <br> // 该函数的返回值实际上是 ILWordExtension 接口数组</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getExtArray() {<br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">array</span><span style="color: #000000;">(<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查权限</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckPowerExtension()</span><span style="color: #000000;">,</span><span style="color: #000000;"> <br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查内容</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckContentExtension()</span><span style="color: #000000;">,</span><span style="color: #000000;"> <br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 加分</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AddScoreExtension()</span><span style="color: #000000;">,</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;"> <br> );<br> }</span><br>};<br></span> </div>
如果还有新需求,,我们只要再添加ILWordExtension 实现类并且把它注册到getExtArray函数里即可。程序从此有了条理,并且算是具备了可扩展性。
不过先不要忙着高兴,有个问题就在这个可扩展性里。当新的需求被提出之后,我们可以再添加 ILWordExtension 实现类,这个的确正确。但是将这个新类注册到getExtArray函数里,等于说还是要修改主业务逻辑代码。能不能不修改呢?每次有新的需求变化还是要告知主业务逻辑,这样终归不太好。最理想的情况是新的扩展代码加入系统之后,主业务逻辑代码不用修改,因为主业务逻辑根本不知道有新扩展这回事!为此我们还需要优化一下设计方案,如图2所示:
(图2),加入扩展家族类
对于调用扩展的主程序(也就是中间服务类LWordServiceCore),只让它知道有ILWordExtension(扩展)这件事就可以了,它不需要知道还有CheckPowerExtension(检查权限扩展)、CheckContentExtension(检查内容扩展)和AddScoreExtension(加分扩展)这三个类。对这三个类的调用过程被移动到LWordExtensionFamily (扩展家族类)里去了。
LWordExtensionFamily其实就是一个能存放多个ILWordExtension接口实例的容器类,从图2中可以看出这个容器类不仅仅是实现了ILWordExtension接口,而且还聚合多个ILWordExtension接口的实例,所以它很特殊!对于LWordServiceCore类,这个类只知道ILWordExtension接口,但并不知道这个接口存在三个实现类。恰好LWordExtensionFamily类就实现了ILWordExtension接口,这很好的符合了中间服务类的要求,并且这个扩展家族类知道ILWordExtension存在三个实现类,并会一一调用它们, LWordExtensionFamily代码大概如代码8所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 8, 扩展家族<br>// 扩展家族</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordExtensionFamily </span><span style="text-decoration: underline;"><span style="color: #0000ff;">implements</span></span><span style="color: #000000;"><span style="text-decoration: underline;"> ILWordExtension</span> {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 扩展数组</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #800080;">$_extensionArray</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">array</span><span style="color: #000000;">();<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 添加扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> addExtension(ILWordExtension </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray []</span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">;<br> }</span><br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">-></span><span style="color: #000000;">beforeAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);<br> }</span><br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">-></span><span style="color: #000000;">behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);<br> }</span><br> }<br>}<br></span> </div>
通过代码8不难看出LWordExtensionFamily类虽然也实现了ILWordExtension接口,但是它并不做任何实质的操作,而是通过循环语句将调用过程一一传递下去。为了平滑实现扩展到插入的方式,所以最好创建一个工厂类MyExtensionFactory。如代码9所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 9<br>// 自定义扩展工厂</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyExtensionFactory {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建留言扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> createLWordExtension() {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #800080;">$lwef</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordExtensionFamily();<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckPowerExtension());<br> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckContentExtension());<br> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AddScoreExtension());<br><br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$lwef</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;"><span style="background-color: #e0e0e0;">;<br> <span style="color: #008000;">// 注意这里返回的是扩展家族类对象, <br> // 扩展家族 LWordExtensionFamily 恰好也实现了接口 ILWordExtension, <br> // 所以这是符合业务逻辑的要求. <br> // 从此, 业务逻辑可以不关心具体的扩展对象, 只要知道扩展家族即可</span></span><br></span> }<br>}<br></span> </div>
使用扩展工厂类的好处就是可以随意的添加和移除扩展实例,这就很好的实现了可插入式编程。对于LWordServiceCore类只知道一个ILWordExtension接口,对于LWordExtensionFamily知道需要一一调用每个扩展,但是具体会有多少个扩展是通过MyExtensionFactory给出的。各负其责结构也很清晰。如果我们做一个假设,MyExtensionFactory类的createLWordExtension函数不是通过new关键字这样的硬编码方式来添加扩展列表,而是通过更巧妙的读取配置文件的方式来得到扩展列表,那么是不是更方便更灵活呢?不过这个就不再本文中讨论了。
中间服务层通过工厂类取得一个ILWordExtension接口的具体实例,然后调用其beforeAppend和behindAppend方法。当然中间服务并不知道工厂类返回的其实是一个含有多个ILWordExtension实例的容器(因为这个容器也实现了ILWordExtension接口),所以中间服务也就不知道扩展是被一一调用的。完整代码如代码10所示:
<div> <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;"> 代码 10, 完整代码<br>// 扩展接口</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查权限</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> CheckPowerExtension </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在这里判断用户权限</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查留言文本</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> CheckContentExtension </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">stristr</span><span style="color: #000000;">(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">fuck</span><span style="color: #000000;">"</span><span style="color: #000000;">))<br> </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">Exception</span><span style="color: #000000;">();<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 用户积分</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AddScoreExtension </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在这里给用户积分</span><span style="color: #008000;"><br></span><span style="color: #000000;"> }<br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 扩展家族</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordExtensionFamily </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordExtension {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 扩展数组</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #800080;">$_extensionArray</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">array</span><span style="color: #000000;">();<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> addExtension(ILWordExtension </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray []</span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">;<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言前</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">-></span><span style="color: #000000;">beforeAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> }<br> }<br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言后</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br> </span><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">_extensionArray </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">) {<br> </span><span style="color: #800080;">$extension</span><span style="color: #000000;">-></span><span style="color: #000000;">behindAppend(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br> }<br> }<br>}<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定义扩展工厂</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyExtensionFactory {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建留言扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> createLWordExtension() {<br> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordExtensionFamily();<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckPowerExtension());<br> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CheckLWordExtension());<br> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">-></span><span style="color: #000000;">addExtension(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AddScoreExtension());<br><br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$lwef</span><span style="color: #000000;">;<br> }<br>}<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;"> 中间服务层</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LWordServiceCore </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> ILWordService {<br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加留言</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">) {<br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #008000;">//</span><span style="color: #008000;"> 获取扩展</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$ext</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> MyExtensionFactory</span><span style="color: #000000;">::</span><span style="color: #000000;">createLWordExtension();<br><br> </span><span style="color: #800080;">$ext</span><span style="color: #000000;">-></span><span style="color: #000000;">beforeAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br><br> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用数据访问层</span><span style="color: #008000;"><br></span><span style="color: #000000;"> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LWordDBTask();<br> </span><span style="color: #800080;">$dbTask</span><span style="color: #000000;">-></span><span style="color: #000000;">append(</span><span style="color: #800080;">$newLWord</span><span style="color: #000000;">);<br><br><span style="background-color: #e0e0e0;"> </span></span><span style="background-color: #e0e0e0;"><span style="color: #800080;">$ext</span><span style="color: #000000;">-></span><span style="color: #000000;">behindAppend(</span><span style="color: #800080;">$newLWord</span></span><span style="color: #000000;"><span style="background-color: #e0e0e0;">);</span><br> }<br>};<br></span> </div>
从代码10中可以看出虽然CheckPowerExtension、CheckContentExtension、AddScoreExtension以及LWordExtensionFamily都实现了ILWordExtension接口,但是它们的beforeAppend和behindAppend函数过程却完全不同!特别是LWordExtensionFamily扩展家族类,它并没有实质的业务逻辑处理过程,而是将调用依次传递给每一个扩展。beforeAppend和behindAppend函数在具体类中的不同实现,这是面向对象程序设计中的很典型的特性:多态!
将次业务逻辑分散到各个扩展中,这种做法已经非常近似AOP(Aspect OrientedProgramming,面向切面编程)的编程方式。权限校验、内容检查和积分可以看作是不同的切面,这些切面和主业务逻辑交叉在一起,但又不会影响到主业务逻……这样做的好处就是扩展代码不会干扰主业务逻辑,我们也可以针对某一个扩展进行编码和单元测试,然后通过MyExtensionFactory工厂类把扩展插入到业务流程中。完整的执行过程如图3所示: