I have a Products class. Now I want to add some kind of discount module to my website that should interact with the Products class.
Currently, the only solution I can come up with is to use some kind of decorator pattern to wrap around the product class so that it can change the price of the product.
Like this:
class Product {
function price() {
return 10;
}
}
class ProductDiscountDecorator {
private $product;
function __construct($product) {
$this->product = $product;
}
function price() {
return $this->product->price()*0.8;
}
}
$product = new ProductDiscountDecorator(new Product());
echo $product->price();
This is a discount, prices must be adjusted on each page of the website. Therefore, each page using the Product class must also add a decorator. The only way to solve this problem is to use a factory that automatically adds this decorator.
$product = $factory->get('product');
Maybe this will work, but I feel like I'm using the decorator pattern incorrectly here.
, , - ? - ?