PHP策略模式demo

<?php
//策略模式就是你有很多的方法,选择一种适合自己的,
// 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,
// 工厂模式就是
//策略模式
interface chargeStrategy{//抽象策略类
public function charge();
}
class Apicharge implements chargeStrategy{//具体策略类
public function charge(){
echo "支付宝支付";
}
}
class Wxcharge implements chargeStrategy{
public function charge(){
echo "微信支付";
}
}

class pzhang{
private $charge;
// public function __construct(ChargeStrategy $chargeStrategy){
// $this->charge = $chargeStrategy;
// }
public function payment($channel){
if($channel == 'Apicharge'){
$this->charge = new Apicharge();
}else if($channel == 'Wxcharge'){
$this->charge = new Wxcharge();
}else{
$this->charge = null;
}
}
public function charge(){
if(is_null($this->charge)){
exit('00');
}
$this->charge->charge();
}
}
//$channel = trim($_GET['channel']);
$obj = new pzhang();
$obj->payment('Apicharge');
$obj->charge();
?>

猜你喜欢

转载自www.cnblogs.com/isuansuan/p/9760419.html