模版模式创建了一个实施一组方法和功能的抽象对象,子类通常将这个对象作为模版用于自己的设计。
超市的商品一般用普通的价格进行销售,如果遇到节日会打折促销,如果遇到突发事件 ,比如水资源紧张,那么矿泉水就会翻倍。
demo1.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
header('Content-Type:text/html; charset=utf-8');
class Template{
private $price = 10;
public function getPrice(){
return $this->price;
}
}
class Sell1 extends Template{
public function getPrice(){
return parent::getPrice()*0.8;
}
}
class Sell2 extends Template{
public function getPrice(){
return parent::getPrice()*2;
}
}
$sell = new Sell1();
echo $sell->getPrice();
echo '<br />';
$sell = new Sell2();
echo $sell->getPrice();