Decorator Pattern

<?php

abstract class Goggle{
abstract function get_impairement_of_judgement();
}

class BeerGoggle extends Goggle{
private $_impairement_of_judgement = 0;
public function get_impairement_of_judgement(){
return $this->_impairement_of_judgement;
}
}

abstract class GoggleDecorator extends Goggle{
protected $_goggle;
public function __construct(Goggle $goggle){
$this->_goggle = $goggle;
}
}

class SixPack extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() + 6;
}
}

class TwelvePack extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() + 12;
}
}

class RedBull extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() -0.5;
}
}

echo '~~~~~~~~~~~~~ Decorator Example: ~~~~~~~~~~~~~';

$goggle = new BeerGoggle();
echo 'Goggles Only Impairment: ' . $goggle->get_impairement_of_judgement() . '%<br>';

$six_beers = new SixPack(new BeerGoggle());
echo '6 Pack Impairment: ' . $six_beers->get_impairement_of_judgement() . '%<br>';

$twelve_beers = new TwelvePack(new BeerGoggle());
echo '12 Pack Impairment: ' . $twelve_beers->get_impairement_of_judgement() . '%<br>';

$eighteen_beer_goggle = new TwelvePack(new SixPack(new BeerGoggle()));
echo '18 Pack Impairment: ' . $eighteen_beer_goggle->get_impairement_of_judgement() . '%<br>';

$six_beers_and_a_redbull = new RedBull(new SixPack(new BeerGoggle()));
echo 'Six Pack and a Redbull: ' . $six_beers_and_a_redbull->get_impairement_of_judgement() . '%<br><br>';

?>

0 Comments

You must be logged in to post a comment.