PHP设计模式之策略模式
<meta charset="utf8">
<?php
//对象注入 依赖注入
interface watcher{
function dazhe();
}
class silver implements watcher {
function dazhe()
{
return 0.9;
}
}
class vip implements watcher {
function dazhe()
{
return 0.8;
}
}
class Goods{
public $price;
function __construct($price)
{
$this->price=$price;
}
function jiage(watcher $watcher){
return $this->price*$watcher->dazhe();
}
}
$goods=new Goods(1200);
echo $goods->jiage(new silver());
echo $goods->jiage(new vip());