function getWealthFactor() { return parent::getWealthFactor() - 4; }
<?php include_once '../../class/pattern/decorator.php'; $tile = new Plains(); echo $tile->getWealthFactor() . "<br>"; $tile = new DiamondDecorator(new Plains()); // 平原+鑽石資源 echo $tile->getWealthFactor() . "<br>"; $tile = new PollutionDecorator(new DiamondDecorator(new Plains())); // 平原+鑽石資源+汙染 echo $tile->getWealthFactor() . "<br>"; //$process = new AuthenticateRequest(new StructureRequest( new LogRequest( new MainProcess()))); //$process->process(new RequestHelper());
{ /** * @return mixed */ function getWealthFactor() { return $this->tile->getWealthFactor() + 2; } } /** * Class PollutionDecorator */ class PollutionDecorator extends TileDecorator { /** * @return mixed */ function getWealthFactor() { return $this->tile->getWealthFactor() - 4; } } $tile = new Plains(); echo $tile->getWealthFactor(); // 2 $tile = new DiamondDecorator(new Plains()); echo $tile->getWealthFactor(); // 4 $tile = new PollutionDecorator(new DiamondDecorator(new Plains())); echo $tile->getWealthFactor(); // 0
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