1、创建抽象策略接口
interface GoodsStrategy{
public function modifyState();
}
2、编写需要的算法类
class SupplerCommodityPoolClass implements GoodsStrategy{
protected $goodsObj;
protected $publicGoodsSaveObj;
public function __construct(PublicGoodsSave $publicGoodsSaveObj,Goods $goodsObj)
{
$this->publicGoodsSaveObj = $publicGoodsSaveObj;
$this->goodsObj = $goodsObj;
}
public function modifyState(){
//做你想要的处理
}
}
class SupplerCommodityPoolClass_1 implements GoodsStrategy{
protected $goodsObj;
protected $publicGoodsSaveObj;
public function __construct(PublicGoodsSave $publicGoodsSaveObj,Goods $goodsObj)
{
$this->publicGoodsSaveObj = $publicGoodsSaveObj;
$this->goodsObj = $goodsObj;
}
public function modifyState(){
//做你想要的处理
}
}
class SupplerCommodityPoolClass_2 implements GoodsStrategy{
protected $goodsObj;
protected $publicGoodsSaveObj;
public function __construct(PublicGoodsSave $publicGoodsSaveObj,Goods $goodsObj)
{
$this->publicGoodsSaveObj = $publicGoodsSaveObj;
$this->goodsObj = $goodsObj;
}
public function modifyState(){
//做你想要的处理
}
}
3、编写配置类
class GoodsConfig{
public $config;
public function __construct(GoodsStrategy $config){
$this->config = $config;
}
public function doWork(){
return $this->config->modifyState();
}
}
4、调用(客户端调用,由客户自己决定使用哪种策略,即客户自行实例化算法类)
//$reClass = new GoodsConfig(new SupplerCommodityPoolClass_1());
//$reClass = new GoodsConfig(new SupplerCommodityPoolClass_2());等
$reClass = new GoodsConfig(new SupplerCommodityPoolClass());
$reClass->doWork();
这样就完美的实现了策略模式