Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Created November 14, 2017 05:44
Show Gist options
  • Save im4aLL/729598e718495f51391769030e9a9674 to your computer and use it in GitHub Desktop.
Save im4aLL/729598e718495f51391769030e9a9674 to your computer and use it in GitHub Desktop.
Design pattern - decorator example
<?php
/**
* PHP decorator pattern example
*/
interface TransactionInterface {
public function getCost();
}
class Transaction implements TransactionInterface {
protected $basePrice = 100;
public function getCost()
{
return $this->basePrice;
}
}
abstract class Product implements TransactionInterface {
protected $transactionInterface;
protected $basePrice = 0;
public function __construct(TransactionInterface $transactionInterface)
{
$this->transactionInterface = $transactionInterface;
}
public function getCost()
{
return $this->transactionInterface->getCost() + $this->basePrice;
}
}
class Pizza extends Product {
protected $basePrice = 500;
}
class Beef extends Product {
protected $basePrice = 300;
}
class Vat extends Product {
protected $basePrice = 15;
public function getCost()
{
return ($this->getAmount() * $this->transactionInterface->getCost() / 100) + $this->transactionInterface->getCost();
}
public function getAmount()
{
return $this->basePrice;
}
}
$transaction = new Transaction();
echo 'Base transaction - ' . $transaction->getCost() . "<br>";
$transaction = new Pizza($transaction);
echo 'Base transaction with Pizza - ' . $transaction->getCost() . "<br>";
$transaction = new Beef($transaction);
echo 'Base transaction with Pizza and Beef - ' . $transaction->getCost() . "<br>";
$total = new Vat($transaction);
echo 'Total with vat (' . $total->getAmount() . '%) - ' . $total->getCost();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment