protected $tile; function __construct(Tile $tile) { $this->tile = $tile; } } class DiamontDecorator extends TileDecorator { function getWealthFactor() { return $this->tile->getWealthFactor() + 5; } } class PollutionDecorator extends TileDecorator { function getWealthFactor() { return $this->tile->getWealthFactor() - 5; } } $tile = new Plains(); $pollution = new PollutionDecorator($tile); print $pollution->getWealthFactor() . "\n"; $army = new Army(); $army->addUnit(new Archer()); $army->addUnit(new LaserCanonUnit()); $sub_army = new Army(); $sub_army->addUnit(new Archer()); $sub_army->addUnit(new LaserCanonUnit()); $army->addUnit($sub_army); print "Атака всей армии составляет {$army->bombardStrength()}";
<?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