Ejemplo n.º 1
0
<?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());
Ejemplo n.º 2
0
{
    /**
     * @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
Ejemplo n.º 3
0
        return $this->tile->getWealthFactor() + 2;
    }
}
class PolutedDecorator extends TileDecorator
{
    function getWealthFactor()
    {
        return $this->tile->getWealthFactor() - 4;
    }
}
// Каждый из этих классов расширяет TileDecorator.
// Это означает, что у них есть ссылка на объект типа Tile.
// Когда вызывается метод getWealthFactor ()...
// каждый из этих классов сначала вызывает такой же метод у объекта типа Tile,
// а затем выполняет собственную корректировку значения.
/*----------------------------------------------*/
$tile = new DiamondDecorator(new Plains());
// В объекте типа DiamondDecorator хранится ссылка на объект типа Plains.
// Перед прибавлением собственного значения +2
// он вызывает метод getWealthFactor() объекта типа Plains
print $tile->getWealthFactor();
echo "<br>";
$tile2 = new PolutedDecorator(new DiamondDecorator(new Plains()));
// так же можно одновременно применять несколько декораторов
print $tile2->getWealthFactor();
echo "<br>";
$tile3 = new PolutedDecorator(new DiamondDecorator(new Forest()));
// так же можем создавать новые типы(Forest например) которые расширяют класс Tile
// и применять к ним декораторы, они не знают какой класс они будут "декорировать",
// главное что это наследник класса Tile
print $tile3->getWealthFactor();