<?php
interface Food{
public function name();
public function price();
}
class ShouZhuaBing implements Food{
public function name()
{
// TODO: Implement name() method.
return "手抓饼";
}
public function price()
{
// TODO: Implement price() method.
return 5;
}
}
abstract class Decorator{
protected $food;
protected $foodname;
protected $foodprice;
public function __construct(Food $food)
{
$this->food=$food;
$this->foodname=$food->name();
$this->foodprice=$food->price();
}
}
class Peicai extends Decorator {
public function name($name)
{
// TODO: Implement name() method.
$this->foodname.="+$name";
return $this->foodname;
}
public function price($price)
{
$this->foodprice+=$price;
return $this->foodprice;
}
}
class Index{
public function run(){
$shouzhuabing=new ShouZhuaBing();
$egg=new Peicai($shouzhuabing);
$eggname=$egg->name('蛋');
$eggprice=$egg->price(1);
$changname=$egg->name('肠');
$changprice=$egg->price(1);
echo "$changname price is $changprice";
}
}
$client=new Index();
$client->run();
装饰模式
最新推荐文章于 2024-09-02 07:00:00 发布