PHP设计模式之:策略模式
<?php
abstract class Strategy
{
public
abstract function AlgorithmInterface();
}
class ConcreteStrategyA
extends Strategy
{
public function
AlgorithmInterface()
{
// 实现细节
}
}
class ConcreteStrategyB
extends Strategy
{
public function
AlgorithmInterface()
{
// 实现细节
}
}
class ConcreteStrategyC
extends Strategy
{
public function
AlgorithmInterface()
{
// 实现细节
}
}
/*
*************************************** */
// 配置
class
Context
{
public $strategy;
public function __construct($strategy)
{
$this->strategy =
$strategy;
}
public function
ContextInterface()
{
$this->strategy->AlgorithmInterface();
}
}
// 实现过程
$context = new Context(new
ConcreteStrategyA());
$context.ContextInterface();
$context = new
Context(new
ConcreteStrategyB());
$context.ContextInterface();
$context = new
Context(new ConcreteStrategyC());
$context.ContextInterface();
/*
********************************* */
// 策略模式具体实现
abstract class
CashSuper
{
public abstract function
acceptCash($money);
}
// 正常收费
class CashNormal extends
CashSuper
{
public function
acceptCash($money)
{
return $money;
}
}
// 打折收费
class
CashRebate extends CashSuper
{
private
$moneyReturn;
public function
__construct($moneyReturn)
{
$this->moneyReturn = $moneyReturn;
}
public function
acceptCash($money)
{
return $money * $this->moneyReturn;
}
}
// 返利收费
class CashReturn extends
CashSuper
{
private $moneyCondtion;
private $moneyReturn;
function __construct($moneyCondition, $moneyReturn)
{
$this->moneyCondtion =
$moneyCondition;
$this->moneyReturn
= $moneyReturn;
}
public
function acceptCash($money)
{
$result = $money;
if($money >= $this->moneyCondtion)
$result = $money -
Math.Float($money/$this->moneyCondition) *
$this->moneyReturn;
return
$result;
}
}
//
实现
class CashContext
{
//
为一个构造实现一子类
private cs;
public
function __construct($cashSuper)
{
$this->cs = $cashSuper;
}
public function
GetResult($money)
{
return cs.acceptCash($money);
}
}
/**
@param $flag 检测收费方式
@param $old 正常价格
@param $back 返回利率
@param $condtion 正常价格
@param $conditionback
应返回的利率
@param $money 实现的金额
*/
$flag;
$old,
$back;
$condtion,
$conditionback;
$money;
switch($flag)
{
case
"normal":
$res = new CashContext(new
CashNormal());
break;
case
"打折":
$res = new CashContext(new
CashRebate($old, $back));
break;
case "返利":
$res = new CashContext(new CashReturn($conditon,
$conditionback));
break;
}
$res.acceptCash($money);
/**
********************************** */
// 策略模式与工厂模式结合,只需修改
CashContext即可
/**
@param $flag
检测收费方式
@param $old 正常价格
@param $back
返回利率
@param $condtion 正常价格
@param
$conditionback 应返回的利率
@param $money
实现的金额
*/
// 用到时把参数修改为实际数字即可
class
CashContext
{
// 为一个构造实现一子类
private cs;
private $flag;
public function __construct($flag)
{
$this->flag = $flag;
}
public function
GetResult($money)
{
switch($this->flag)
{
case
"normal":
$res = new CashContext(new
CashNormal());
break;
case "打折":
$res = new CashContext(new
CashRebate($old, $back));
break;
case "返利":
$res = new CashContext(new
CashReturn($conditon, $conditionback));
break;
}
return
$res.acceptCash($money);
}
}
$flag;
$res =
new CashContext($flag);
$res.acceptCash($money);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。