<?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);
相关文章
- 04-10(设计模式)命令 > 本篇文章由一文多发平台[ArtiPub](https://github.co
- 04-10大话设计模式----中介者模式
- 04-10设计模式-迭代器模式
- 04-10设计模式-访问者模式
- 04-10设计模式之美学习-结构型-装饰者模式(二十)
- 04-10设计模式-状态模式
- 04-10大话设计模式之<一>计算器的深思
- 04-10设计模式(九)装饰模式
- 04-10设计模式---策略模式
- 04-10Head First之策略模式