Created
October 17, 2019 12:30
-
-
Save felipevolpatto/b7a6a5109f2d383711088cc66a462c7f to your computer and use it in GitHub Desktop.
Design patterns: 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 | |
use Decorator\Product; | |
use Decorator\Decorators\ProductAmazonDecorator; | |
use Decorator\Decorators\ProductMercadoLivreDecorator; | |
/* No contexto de produto simples */ | |
$product = new Product(); | |
$product->validate(); | |
// Output | |
// Validação do produto em si. | |
/* No contexto de envio para a Amazon */ | |
$product = new Product(); | |
$product = new ProductAmazonDecorator($product); | |
$product->validate(); | |
// Output | |
// Validação do produto em si. | |
// Validação do produto na Amazon. | |
/* No contexto de envio para a Mercado Livre */ | |
$product = new Product(); | |
$product = new ProductMercadoLivreDecorator($product); | |
$product->validate(); | |
// Output | |
// Validação do produto em si. | |
// Validação do produto no Mercado Livre. | |
/* No contexto de validação para todos */ | |
$product = new Product(); | |
$product = new ProductAmazonDecorator($product); | |
$product = new ProductMercadoLivreDecorator($product); | |
$product->validate(); | |
// Output | |
// Validação do produto em si. | |
// Validação do produto na Amazon. | |
// Validação do produto no Mercado Livre. |
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 | |
namespace Decorator; | |
use Decorator\ValidationInterface; | |
class Product implements ValidationInterface { | |
public function validate(): bool { | |
echo 'Validação do produto em si.'; | |
return true; | |
} | |
} |
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 | |
namespace Decorator\Decorators; | |
use Decorator\Decorators\ProductDecorator; | |
class ProductAmazonDecorator extends ProductDecorator { | |
public function validate(): bool { | |
$this->product->validate(); | |
echo 'Validação do produto na Amazon.'; | |
return true; | |
} | |
} |
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 | |
namespace Decorator\Decorators; | |
use Decorator\ValidationInterface; | |
abstract class ProductDecorator implements ValidationInterface { | |
protected $product; | |
public function __construct(ValidationInterface $product) { | |
$this->product = $product; | |
} | |
abstract function validate(): bool; | |
} |
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 | |
namespace Decorator\Decorators; | |
use Decorator\Decorators\ProductDecorator; | |
class ProductMercadoLivreDecorator extends ProductDecorator { | |
public function validate(): bool { | |
$this->product->validate(); | |
echo 'Validação do produto no Mercado Livre.'; | |
return true; | |
} | |
} |
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 | |
namespace Decorator; | |
interface ValidationInterface { | |
public function validate(): bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment