Home>Article>Backend Development> PHP Strategy Pattern
The content shared with you in this article is about PHP strategy model, which has certain reference value. Friends in need can refer to it
Implement a shopping mall cashier system. Products can have normal charging, discount charging, rebate charging and other modes (from "Dahua Design Pattern")
According to the requirements, the charging operation can be designed as an interface algorithm. Normal charging, discount charging, and rebate charging all inherit this interface to implement different strategies. algorithm. Then design an environment class to maintain instances of the policy.
http://download.csdn.net/detail/clevercode/8700009
1) strategy.php
[php]view plain copy
_moneyRebate = $rebate; } /** * 返回正常金额 * * @param double $money 金额 * @return double 金额 */ public function acceptCash($money){ return $this->_moneyRebate * $money; } } // 返利策略 class ReturnStrategy implements ICashStrategy{ // 返利条件 private $_moneyCondition = null; // 返利多少 private $_moneyReturn = null; /** * 构造函数 * * @param double $moneyCondition 返利条件 * @return double $moneyReturn 返利多少 * @return void */ public function __construct($moneyCondition, $moneyReturn){ $this->_moneyCondition = $moneyCondition; $this->_moneyReturn = $moneyReturn; } /** * 返回正常金额 * * @param double $money 金额 * @return double 金额 */ public function acceptCash($money){ if (!isset($this->_moneyCondition) || !isset($this->_moneyReturn) || $this->_moneyCondition == 0) { return $money; } return $money - floor($money / $this->_moneyCondition) * $this->_moneyReturn; } }
2) strategyPattern.php
view plain copy
setCashStrategy($type); } /** * 设置策略(简单工厂与策略模式混合使用) * * @param string $type 类型 * @return void */ public function setCashStrategy($type){ $cs = null; switch ($type) { // 正常策略 case 'normal' : $cs = new NormalStrategy(); break; // 打折策略 case 'rebate8' : $cs = new RebateStrategy(0.8); break; // 返利策略 case 'return300to100' : $cs = new ReturnStrategy(300, 100); break; } $this->_strategy = $cs; } /** * 获取结果 * * @param double $money 金额 * @return double */ public function getResult($money){ return $this->_strategy->acceptCash($money); } /** * 获取结果 * * @param string $type 类型 * @param int $num 数量 * @param double $price 单价 * @return double */ public function getResultAll($type, $num, $price){ $this->setCashStrategy($type); return $this->getResult($num * $price); } } /* * 客户端类 * 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合, * 使业务逻辑的算法更具有可移植性 */ class Client{ public function main(){ $total = 0; $cashContext = new CashContext(); // 购买数量 $numA = 10; // 单价 $priceA = 100; // 策略模式获取结果 $totalA = $cashContext->getResultAll('normal', $numA, $priceA); $this->display('A', 'normal', $numA, $priceA, $totalA); // 购买数量 $numB = 5; // 单价 $priceB = 100; // 打折策略获取结果 $totalB = $cashContext->getResultAll('rebate8', $numB, $priceB); $this->display('B', 'rebate8', $numB, $priceB, $totalB); // 购买数量 $numC = 10; // 单价 $priceC = 100; // 返利策略获取结果 $totalC = $cashContext->getResultAll('return300to100', $numC, $priceC); $this->display('C', 'return300to100', $numC, $priceC, $totalC); } /** * 打印 * * @param string $name 商品名 * @param string $type 类型 * @param int $num 数量 * @param double $price 单价 * @return double */ public function display($name, $type, $num, $price, $total){ echo date('Y-m-d H:i:s') . ",$name,[$type],num:$num,price:$price,total:$total\r\n"; } } /** * 程序入口 */ function start(){ $client = new Client(); $client->main(); } start(); ?>
The above is the detailed content of PHP Strategy Pattern. For more information, please follow other related articles on the PHP Chinese website!