1/
<?php
interface AbstractStrategy{
function task();
}
class ConcreteStrategy1 implements AbstractStrategy{
public function task() {
echo "Strategy 1";
}
}
class ConcreteStrategy2 implements AbstractStrategy{
public function task() {
echo "Strategy 2";
}
}
class Context{
private $strategy;
public function setStrategy(AbstractStrategy $obj) {
$this->strategy=$obj;
}
public function getStrategy() {
return $this->strategy;
}
}
// testy
$obj = new Context();
$obj->setStrategy(new ConcreteStrategy1);
$obj->getStrategy()->task(); // wyswietla „Strategy 1”
$obj->setStrategy(new ConcreteStrategy2);
$obj->getStrategy()->task(); // wyswietla „Strategy 2”
?>
2/
<?php
interface Tax{
public function count($net);
}
class TaxPL implements Tax{
public function count($net) {
return 0.23*$net;
}
}
class TaxEN implements Tax{
public function count($net) {
return 0.15*$net;
}
}
class TaxDE implements Tax{
public function count($net) {
return 0.3*$net;
}
}
class Context{
private $tax;
/**
* @return mixed
*/
public function getTax()
{
return $this->tax;
}
/**
* @param mixed $tax
*/
public function setTax(Tax $tax)
{
$this->tax = $tax;
}
}
// testy
$tax = new Context();
$tax->setTax(new TaxPL());
echo $tax->getTax()->count(100); // wyswietla "23"
$tax->setTax(new TAXEN());
echo $tax->getTax()->count(100); // wyswietla "15"
$tax->setTax(new TAXDE());
echo $tax->getTax()->count(100); // wyswietla "30"
?>
<?php
interface Promotion {
public function getPriseAfterPromotion(float $price): float;
}
class BlackFriday implements Promotion {
public function getPriseAfterPromotion(float $price): float {
return $price * 0.2;
}
}
class SpanishKangaroosFestival implements Promotion {
public function getPriseAfterPromotion(float $price): float {
$discount = floor($price / 10) * 2;
return $price - $discount;
}
}
class Checkout {
/**
* @var type
*/
private $price;
/**
* @var Promotion
*/
private $promotion;
/**
* @param float $price
*/
public function setPrice(float $price): void {
$this->prise = $price;
}
/**
* @param Promotion $promition
*/
public function setPromotion(Promotion $promition): void {
$this->promition = $promition;
}
/**
* @return float
*/
public function getTotalPrice(): float {
return $this->promition->getPriseAfterPromotion($this->prise);
}
}
$checkout = new Checkout();
$checkout->setPrice(22.333);
$checkout->setPromotion(new BlackFriday());
echo $checkout->getTotalPrice() . ' zl';
echo '<br><br>';
$checkout2 = new Checkout();
$checkout2->setPrice(120.9);
$checkout2->setPromotion(new SpanishKangaroosFestival());
echo $checkout2->getTotalPrice() . ' zl';