Created
April 11, 2016 23:01
-
-
Save donkarlo/ebd5c43eefdb1e686c500e48f9215a77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment