<?php abstract class Tile { // 砖瓦 abstract function getWealthFactor(); // 获取财富 } class Plains extends Tile { // 平原 private $wealthfactor = 2; function getWealthFactor() { return $this->wealthfactor; } } class DiamondPlains extends Plains { // 钻石地段 function getWealthFactor() { return parent::getWealthFactor() + 2; } } class PollutedPlains extends Plains { // 污染地段 function getWealthFactor() { return parent::getWealthFactor() - 4; } } $tile = new PollutedPlains(); print $tile->getWealthFactor(); ?>
output:
-2
点评:不具有灵活性,我们不能同时获得钻石与被污染的土地的资金情况。
装饰模式使用组合和委托而不是只使用继承来解决功能变化的问题。
看例子:
<?php abstract class Tile { abstract function getWealthFactor(); } class Plains extends Tile { private $wealthfactor = 2; function getWealthFactor() { return $this->wealthfactor; } } abstract class TileDecorator extends Tile { // 装饰 protected $tile; function __construct( Tile $tile ) { $this->tile = $tile; } } class DiamondDecorator extends TileDecorator { // 钻石装饰 function getWealthFactor() { return $this->tile->getWealthFactor()+2; } } class PollutionDecorator extends TileDecorator { // 污染装饰 function getWealthFactor() { return $this->tile->getWealthFactor()-4; } } $tile = new Plains(); print $tile->getWealthFactor(); // 2 $tile = new DiamondDecorator( new Plains() ); print $tile->getWealthFactor(); // 4 $tile = new PollutionDecorator( new DiamondDecorator( new Plains() )); print $tile->getWealthFactor(); // 0 ?>
output:
2
4
0
点评:这个模型具有扩展性。我们不需要创建DiamondPollutionPlains对象就可以构建一个钻石被污染的对象。
一个更逼真的例子
<?php class RequestHelper{} // 请求助手 abstract class ProcessRequest { // 进程请求 abstract function process( RequestHelper $req ); } class MainProcess extends ProcessRequest { // 主进程 function process( RequestHelper $req ) { print __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 __CLASS__.": logging request\n"; // 当前类,有点递归的感觉 $this->processrequest->process( $req ); } } class AuthenticateRequest extends DecorateProcess { // 认证请求 function process( RequestHelper $req ) { print __CLASS__.": authenticating request\n"; $this->processrequest->process( $req ); } } class StructureRequest extends DecorateProcess { // 组织结构请求 function process( RequestHelper $req ) { print __CLASS__.": structuring request\n"; $this->processrequest->process( $req ); } } $process = new AuthenticateRequest( new StructureRequest( new LogRequest ( new MainProcess() ))); // 这样可以很灵活的组合进程的关系,省去很多重复的继承 $process->process( new RequestHelper() ); print_r($process); ?>
output:
AuthenticateRequest: authenticating request StructureRequest: structuring request LogRequest: logging request MainProcess: doing something useful with request AuthenticateRequest Object ( [processrequest:protected] => StructureRequest Object ( [processrequest:protected] => LogRequest Object ( [processrequest:protected] => MainProcess Object ( ) ) ) )
点评:这里有一种递归的感觉,一层调用一层。模式是牛人总结出来用于灵活的解决一些现实问题的。牛!给开发多一点思路。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》