Skip to content

Instantly share code, notes, and snippets.

@kandran
Last active August 29, 2015 14:18
Show Gist options
  • Save kandran/149a41a369dc65221104 to your computer and use it in GitHub Desktop.
Save kandran/149a41a369dc65221104 to your computer and use it in GitHub Desktop.
Design pattern Décorateur (http://kandran.fr/design-pattern-decorateur/)
<?php
require_once("Ship.php");
class Accelerator extends Ship
{
protected $ship;
public function __construct(Ship $ship)
{
$this->ship = $ship;
$this->speed = 10;
}
public function getSpeed()
{
return $this->speed + $this->ship->getSpeed();
}
}
<?php
require_once("Ship.php");
class JediShip extends Ship
{
public function __construct()
{
$this->speed = 100;
}
}
<?php
abstract class Ship
{
protected $speed;
public function getSpeed()
{
return $this->speed;
}
}
<?php
require_once("JediShip.php");
require_once("Accelerator.php");
$jediShipObject = new JediShip(); // extends of Ship
$jediShipObject->getSpeed(); // return 100
$jediShipObject = new Accelerator($jediShipObject); // extends of Ship
$jediShipObject->getSpeed(); // return 110
$jediShipObject = new Accelerator($jediShipObject); // extends of Ship
$jediShipObject->getSpeed(); // return 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment