Created
July 26, 2019 09:21
-
-
Save fjugaldev/fda87196efa5d5675bf885ebb03b3ede to your computer and use it in GitHub Desktop.
PHP Decorator Pattern
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 | |
$macbook = new Macbook(); | |
echo 'Una Macbook con pantalla de 13 pulgadas cuesta ' . $macbook->getPrice() . ' euros' . PHP_EOL; | |
$macbook15Pulg = new WithDisplaySize15(new Macbook()); | |
echo 'Una Macbook con pantalla de 15 pulgadas cuesta ' . $macbook15Pulg->getPrice() . ' euros' . PHP_EOL; | |
$macbook17Pulg = new WithDisplaySize17(new Macbook()); | |
echo 'Una Macbook con pantalla de 17 pulgadas cuesta ' . $macbook17Pulg->getPrice() . ' euros' . PHP_EOL; | |
$macbookPro = new MacbookPro(new Macbook()); | |
echo 'Una Macbook Pro con pantalla de 13 pulgadas cuesta ' . $macbookPro->getPrice() . ' euros' . PHP_EOL; | |
$macbookPro15Pulg = new WithDisplaySize15(new MacbookPro(new Macbook())); | |
echo 'Una Macbook Pro con pantalla de 15 pulgadas cuesta' . $macbookPro15Pulg->getPrice() . ' euros' . PHP_EOL; | |
$macbookPro17Pulg = new WithDisplaySize17(new MacbookPro(new Macbook())); | |
echo 'Una Macbook Pro con patanlla de 17 pulgadas cuesta ' . $macbookPro17Pulg->getPrice() . ' euros' . PHP_EOL; |
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 | |
class Macbook | |
{ | |
protected $basePrice = 1500; | |
public function getPrice() | |
{ | |
return $this->basePrice; | |
} | |
} |
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 MacbookDecorator | |
{ | |
protected $price; | |
protected $macbook; | |
public function __construct($macbook) | |
{ | |
$this->macbook = $macbook; | |
} | |
public function getPrice() | |
{ | |
return $this->macbook->getPrice() + $this->price; | |
} | |
} |
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 | |
class MacbookPro extends MacbookDecorator | |
{ | |
protected $price = 950; | |
} |
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 | |
class WithDisplaySize15 extends MacbookDecorator | |
{ | |
protected $price = 350; | |
} |
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 | |
class WithDisplaySize17 extends MacbookDecorator | |
{ | |
protected $price = 700; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment