Last active
August 29, 2015 14:18
-
-
Save kandran/149a41a369dc65221104 to your computer and use it in GitHub Desktop.
Design pattern Décorateur (http://kandran.fr/design-pattern-decorateur/)
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 | |
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(); | |
} | |
} |
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 | |
require_once("Ship.php"); | |
class JediShip extends Ship | |
{ | |
public function __construct() | |
{ | |
$this->speed = 100; | |
} | |
} |
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 Ship | |
{ | |
protected $speed; | |
public function getSpeed() | |
{ | |
return $this->speed; | |
} | |
} |
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 | |
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