Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Boisson
- {
- /**
- * @var float
- */
- protected $valeur;
- /**
- * @var string
- */
- protected $description;
- public function __construct()
- {
- }
- public function __toString()
- {
- return $this->getDescription() . ' (' . $this->getValeur() . ' €)';
- }
- /**
- * @return mixed
- */
- public function getDescription()
- {
- return $this->description;
- }
- /**
- * @param mixed $description
- */
- public function setDescription($description)
- {
- $this->description = $description;
- }
- /**
- * @return float
- */
- public function getValeur()
- {
- return $this->valeur;
- }
- /**
- * @param float $valeur
- */
- public function setValeur($valeur)
- {
- $this->valeur = $valeur;
- }
- /**
- * @param string $supplement
- * @return Boisson
- */
- public function ajoutSupplement($supplement)
- {
- return new $supplement($this);
- }
- }
- class Soda extends Boisson
- {
- public function __construct()
- {
- parent::__construct();
- $this->description = 'Soda';
- $this->valeur = 4.0;
- }
- }
- abstract class SupplementDecorateur extends Boisson
- {
- /**
- * @var Boisson
- */
- protected $boisson;
- public function __construct(Boisson $boisson)
- {
- $this->boisson = $boisson;
- }
- }
- class SupplementMenthe extends SupplementDecorateur
- {
- public function getDescription()
- {
- return $this->boisson->getDescription() . ' + Menthe';
- }
- public function getValeur()
- {
- return $this->boisson->getValeur() + .20;
- }
- }
- class SupplementCitron extends SupplementDecorateur
- {
- public function getDescription()
- {
- return $this->boisson->getDescription() . ' + Citron';
- }
- public function getValeur()
- {
- return $this->boisson->getValeur() + .05;
- }
- }
- class SupplementXL extends SupplementDecorateur
- {
- public function getDescription()
- {
- return $this->boisson->getDescription() . ' + XL';
- }
- public function getValeur()
- {
- return $this->boisson->getValeur() * 1.30;
- }
- }
- $boisson = (new Soda())
- ->ajoutSupplement(SupplementMenthe::class)
- ->ajoutSupplement(SupplementCitron::class)
- ->ajoutSupplement(SupplementCitron::class)
- ->ajoutSupplement(SupplementXL::class);
- print ($boisson);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement