Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class Boisson
  4. {
  5. /**
  6. * @var float
  7. */
  8. protected $valeur;
  9. /**
  10. * @var string
  11. */
  12. protected $description;
  13.  
  14. public function __construct()
  15. {
  16. }
  17.  
  18. public function __toString()
  19. {
  20. return $this->getDescription() . ' (' . $this->getValeur() . ' &euro;)';
  21. }
  22.  
  23. /**
  24. * @return mixed
  25. */
  26. public function getDescription()
  27. {
  28. return $this->description;
  29. }
  30.  
  31. /**
  32. * @param mixed $description
  33. */
  34. public function setDescription($description)
  35. {
  36. $this->description = $description;
  37. }
  38.  
  39. /**
  40. * @return float
  41. */
  42. public function getValeur()
  43. {
  44. return $this->valeur;
  45. }
  46.  
  47. /**
  48. * @param float $valeur
  49. */
  50. public function setValeur($valeur)
  51. {
  52. $this->valeur = $valeur;
  53. }
  54.  
  55. /**
  56. * @param string $supplement
  57. * @return Boisson
  58. */
  59. public function ajoutSupplement($supplement)
  60. {
  61. return new $supplement($this);
  62. }
  63.  
  64. }
  65.  
  66. class Soda extends Boisson
  67. {
  68.  
  69. public function __construct()
  70. {
  71. parent::__construct();
  72. $this->description = 'Soda';
  73. $this->valeur = 4.0;
  74. }
  75.  
  76.  
  77. }
  78.  
  79. abstract class SupplementDecorateur extends Boisson
  80. {
  81.  
  82. /**
  83. * @var Boisson
  84. */
  85. protected $boisson;
  86.  
  87. public function __construct(Boisson $boisson)
  88. {
  89. $this->boisson = $boisson;
  90. }
  91.  
  92. }
  93.  
  94. class SupplementMenthe extends SupplementDecorateur
  95. {
  96.  
  97. public function getDescription()
  98. {
  99. return $this->boisson->getDescription() . ' + Menthe';
  100. }
  101.  
  102.  
  103. public function getValeur()
  104. {
  105. return $this->boisson->getValeur() + .20;
  106. }
  107.  
  108. }
  109.  
  110. class SupplementCitron extends SupplementDecorateur
  111. {
  112.  
  113. public function getDescription()
  114. {
  115. return $this->boisson->getDescription() . ' + Citron';
  116. }
  117.  
  118.  
  119. public function getValeur()
  120. {
  121. return $this->boisson->getValeur() + .05;
  122. }
  123.  
  124. }
  125.  
  126. class SupplementXL extends SupplementDecorateur
  127. {
  128.  
  129. public function getDescription()
  130. {
  131. return $this->boisson->getDescription() . ' + XL';
  132. }
  133.  
  134.  
  135. public function getValeur()
  136. {
  137. return $this->boisson->getValeur() * 1.30;
  138. }
  139.  
  140. }
  141.  
  142.  
  143. $boisson = (new Soda())
  144. ->ajoutSupplement(SupplementMenthe::class)
  145. ->ajoutSupplement(SupplementCitron::class)
  146. ->ajoutSupplement(SupplementCitron::class)
  147. ->ajoutSupplement(SupplementXL::class);
  148.  
  149. print ($boisson);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement