search
HomeBackend DevelopmentPHP TutorialPHP三层结构(下) PHP实现AOP_PHP

本文源码下载地址:http://xiazai.bitsCN.com/201007/yuanma/TraceLWord.rar
开发环境为 eclipse(pdt)
让我们把注意力集中到中间服务层上来。中间服务层代码比较简单,只是调用数据访问层代码将留言保存到数据库。如代码1所示:
复制代码 代码如下:
// 代码 1
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
}
};

在看到留言板的演示之后,公司的产品部和市场部或许会提出各种各样的想法和需求。比如他们希望在添加留言之前判断用户的权限!只有注册用户才能留言!我们需要修改代码,如代码2所示:
复制代码 代码如下:
// 代码 2, 增加登录验证
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
if (!($userLogin)) {
// 提示用户登录
}
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
}
};

市场部又希望在添加留言之前,对留言内容进行检查,如果留言中含有脏话就不保存。我们继续修改代码,如代码3所示:
复制代码 代码如下:
// 代码 3, 增加脏话过滤
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
if (!($userLogin)) {
// 提示用户登录
}
if (stristr($newLWord, "SB")) {
// 含有脏话, 提示留言发送失败
}
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
}
};

产品部也提出了新需求,他们希望加入积分机制。具体来讲就是在用户每次留言成功以后给用户+5分。我们继续修改代码,如代码4所示:
复制代码 代码如下:
// 代码 4, 加入留言积分机制
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
if (!($userLogin)) {
// 提示用户登录
}
if (stristr($newLWord, "SB")) {
// 含有脏话, 提示留言发送失败
}
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
// 给用户加分
$score = getUserScore($userName);
$score = $score + 5;
saveUserScore($userName, $score);
}
};

没过多久,产品部又对需求进行细化,他们希望用户积分每积累够1000分以后,就给用户升级。我们继续修改代码,如代码5所示:
复制代码 代码如下:
// 代码 5, 加入用户升级规则
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
if (!($userLogin)) {
// 提示用户登录
}
if (stristr($newLWord, "fuck")) {
// 含有脏话, 提示留言发送失败
}

// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
// 给用户加分
$score = getUserScore($userName);
$score = $score + 5;
saveUserScore($userName, $score);
// 给用户升级
if (($score % 1000) == 0) {
$level = getUserLevel($userName);
$level = $level + 1;
saveUserLevel($userName, $level);
}
}
};

随着需求的增多,我们需要不断的修改中间服务层代码。但是你应该不难发现,需求越多中间服务层代码也就越多越庞大!最后会导致即便我们使用三层结构的开发模式,也还是没有有效的降低工程难度!另外就是应需求的变化而修改中间服务代码以后,需要重新测试所有代码,而不是有效的测试新增代码……

其实让我们仔细分析一下这个留言板代码,我先要提出一个主业务逻辑和次业务逻辑的概念。无论怎样,把留言内容存入到数据库,这是业务逻辑的主干!这个就是主业务逻辑!这部分没有随着需求的增加而修改。至于在存入数据库之前要进行权限校验,要进行内容检查,存入数据库之后要给用户加分,然后给用户升级,这些都是前序工作和扫尾工作,都是次业务逻辑!主业务逻辑几乎是一成不变的,次业务逻辑变化却非常频繁。为了提高代码的可读性和可维护性,我们可以考虑把这些次业务逻辑放到别的地方,尽量不要让它们干扰主业务逻辑。主业务逻辑专心干自己该干的事情好了,至于别的任何事情,主业务逻辑一概都不闻不问!那么我们的代码就可以写成这样,如代码6所示:
复制代码 代码如下:
// 代码 6, 将主业务逻辑和次业务逻辑分开
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
// 添加留言前
beforeAppend($newLWord);
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
// 添加留言后
behindAppend($newLWord);
}
};

我们可以把权限判断代码和留言内容文本过滤代码统统塞进beforeAppend函数,把用户积分代码塞进behindAppend函数,这样就把次业务逻辑从主业务逻辑代码中清理掉了。主业务逻辑知道有个“序曲”函数beforeAppend,有个“尾声”函数behindAppend,但是在序曲和尾声函数中具体都做了什么事情,主业务逻辑并不知道,也不需要知道!当然实际编码工作并不那么简单,我们还要兼顾产品部和市场部更多的需求变化,所以最好能实现一种插件方式来应对这种变化,但是仅仅依靠两个函数beforeAppend和behindAppend是达不到这个目的~

想要实现插件方式,可以建立接口!使用接口的好处是可以将定义和实现隔离,另外就是实现多态。我们建立一个留言扩展接口ILWordExtension,该接口有两个函数beforeAppend和behindAppend。权限校验、内容检查、加分这些功能可以看作是实现ILWordExtension接口的三个实现类,主业务逻辑就依次遍历这三个实现类,来完成次业务逻辑。如图1所示:
CheckPowerExtension扩展类用作用户权限校验,CheckContentExtension扩展类用作留言内容检查,AddScoreExtension扩展类用作给用户加分和升级。示意代码如代码7所示:
PHP三层结构(下) PHP实现AOP_PHP
(图1),加入扩展接口
复制代码 代码如下:
// 代码 7,加入扩展接口
// 扩展接口
interface ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord);
// 添加留言后
public function behindAppend($newLWord);
};

// 检查权限
class CheckPowerExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
// 在这里判断用户权限
}

// 添加留言后
public function behindAppend($newLWord) {
}
};

// 检查留言文本
class CheckContentExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
if (stristr($newLWord, "SB")) {
throw new Exception();
}
}

// 添加留言后
public function behindAppend($newLWord) {
}
};

// 用户积分
class AddScoreExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
}

// 添加留言后
public function behindAppend($newLWord) {
// 在这里给用户积分
}
};

// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
// 添加留言前
$this->beforeAppend($newLWord);

// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);

// 添加留言后
$this->behindAppend($newLWord);
}

// 添加留言前
private function beforeAppend($newLWord) {
// 获取扩展数组
$extArray = $this->getExtArray();

foreach ($extArray as $ext) {
// 遍历每一个扩展, 并调用其 beforeAppend 函数
$ext->beforeAppend($newLWord);
}
}

// 添加留言后
private function behindAppend($newLWord) {
// 获取扩展数组
$extArray = $this->getExtArray();

foreach ($extArray as $ext) {
// 遍历每一个扩展, 并调用其 behindAppend 函数
$ext->behindAppend($newLWord);
}
}

// 获取扩展数组,
// 该函数的返回值实际上是 ILWordExtension 接口数组
private function getExtArray() {
return array(
// 检查权限
new CheckPowerExtension(),
// 检查内容
new CheckContentExtension(),
// 加分
new AddScoreExtension(),
);
}
};

如果还有新需求,,我们只要再添加ILWordExtension 实现类并且把它注册到getExtArray函数里即可。程序从此有了条理,并且算是具备了可扩展性。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),