Skip to content

Instantly share code, notes, and snippets.

@donkarlo
Created April 11, 2016 23:01
Show Gist options
  • Save donkarlo/ebd5c43eefdb1e686c500e48f9215a77 to your computer and use it in GitHub Desktop.
Save donkarlo/ebd5c43eefdb1e686c500e48f9215a77 to your computer and use it in GitHub Desktop.
<?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