Last active
December 17, 2015 14:49
-
-
Save phcostabh/5627673 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
interface Pizza | |
{ | |
public function getDescription(); | |
public function getPrice(); | |
} | |
class PlainPizza implements Pizza | |
{ | |
public function getDescription() | |
{ | |
return 'Massa'; | |
} | |
public function getPrice() | |
{ | |
return 15.00; | |
} | |
} | |
class Topping implements Pizza | |
{ | |
protected $_pizza; | |
public function __construct(Pizza $new_pizza) | |
{ | |
$this->_pizza = $new_pizza; | |
} | |
public function getDescription() | |
{ | |
return $this->_pizza->getDescription(); | |
} | |
public function getPrice() | |
{ | |
return $this->_pizza->getPrice(); | |
} | |
} | |
class Mozarella extends Topping | |
{ | |
public function getDescription(){ | |
return $this->_pizza->getDescription() . ', Mozarella'; | |
} | |
public function getPrice() | |
{ | |
return $this->_pizza->getPrice() + 5.00; | |
} | |
} | |
class Ham extends Topping | |
{ | |
public function getDescription(){ | |
return $this->_pizza->getDescription() . ', Ham'; | |
} | |
public function getPrice() | |
{ | |
return $this->_pizza->getPrice() + 5.00; | |
} | |
} | |
$my_pizza = new Mozarella(new PlainPizza); | |
echo $my_pizza->getDescription(), PHP_EOL; | |
echo $my_pizza->getPrice(), PHP_EOL; | |
$ham = new Ham($my_pizza); | |
echo $ham->getDescription(), PHP_EOL; | |
echo $ham->getPrice(), PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment