abstract class beverage { private $description; function getDescription() { return $this->description; } abstract function cost(); } class coffA extends beverage { function getDescription() { return "this is a coff A"; } function cost() { return 0.5; } } class coffB extends beverage { function getDescription() { return "this is a coff B"; } function cost() { return 1.5; } } abstract class decorator extends beverage{} class mocca extends beverage { private $baverage; function mocca($baverage) { $this->baverage = $baverage; } function getDescription() { return $this->baverage->getDescription()." with mocha"; } function cost() { return $this->baverage->cost() + 2; } } class milk extends beverage { private $baverage; function milk($baverage) { $this->baverage = $baverage; } function getDescription() { return $this->baverage->getDescription()." with milk"; } function cost() { return $this->baverage->cost() + 3; } } $me = new coffA(); $me = new mocca($me); $me = new milk($me); echo $me->getDescription(); echo $me->cost();
先留点代码上去,以后慢慢完善。