I'm now studying the decorator pattern, here some example code (PHP) :
abstract class component{
public function drawShape(){};
}
class concreteComponent extends component{
public function drawShape(){//code};
}
class decoratorComponent extends component{
private $component;
public function __construct($component){ $this->component=$component; }
public function drawShape(){
$this->component->drawShape();
}
}
class borderDecorator extends decoratorComponent{
public function drawShape(){
$this->drawBorder();
$this->component->drawShape();
}
public function setBorder(){};
public function drawBorder(){};
}
class bgColorDecorator extends decoratorComponent{
public function drawShape(){
$this->drawBgColor();
$this->component->drawShape();
}
public function setbgColor(){};
public function drawBgColor(){};
}
Ok, now:
$test=new concreteComponent();
$border=new borderDecorator($test);
$border->setBorder(10);
$bgColor= new bgColorDecorator($border);
$bgColor->setBgColor(#000);
Now I have a component decorated with a #000 bg color and a 10(some unit) border.
With
$bgColor->drawShape();
it means drawBgColor
+ drawBorder
+ drawShape
and all right, BUT:
How can I modify or remove the border??
$bgColor-> ???
The bgColor class can't access directly the border methods...
Thanks
Aucun commentaire:
Enregistrer un commentaire