本文实例讲述了php设计模式之装饰模式。分享给大家供大家参考,具体如下:
这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
具体装饰(Concrete Decorator)角色:负责给构件对象增加附加的职责。
class RequestHelper{}
abstract class ProcessRequest{
abstract function process(RequestHelper $req);
}
class MainProcess extends ProcessRequest{
function process(RequestHelper $req)
{
print detest_class.": doing something useful with request\n";
}
}
abstract class DecorateProcess extends ProcessRequest{
protected $processRequest;
function __construct(ProcessRequest $pr)
{
$this->processRequest = $pr;
}
}
class LogRequest extends DecorateProcess{
function process(RequestHelper $req)
{
print detest_class.": logging request\n";
$this->processRequest->process($req);
}
}
class AuthenticateRequest extends DecorateProcess{
function process(RequestHelper $req)
{
print detest_class.": authenticating request\n";
$this->processRequest->process($req);
}
}
class StructureRequest extends DecorateProcess{
function process(RequestHelper $req)
{
print detest_class.": structuring request\n";
$this->processRequest->process($req);
}
}
$process = new AuthenticateRequest(new StructureRequest(
new LogRequest(
new MainProcess()
)));
$process->process(new RequestHelper());
Request: authenticating request
StructureRequest: structuring request
LogRequest: logging request
MainProcess: doing something useful with requestphp面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。