本篇文章给大家分享的内容是关于PHP策略模式,有着一定的参考价值,有需要的朋友可以参考一下
例如:一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示不同的广告
6 项目应用
6.1 需求说明
实现一个商场收银系统,商品可以有正常收费,打折收费,返利收费等模式(来之《大话设计模式》)
6.2 需求分析
按照需求,可以将收费操作设计成为一个接口算法,正常收费,打折收费,返利收费都继承这个接口,实现不同的策略算法。然后设计一个环境类,去维护策略的实例。
6.3 设计架构图

6.4 程序源码下载
http://download.csdn.net/detail/clevercode/8700009
6.5 程序说明
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
立即学习“PHP免费学习笔记(深入)”;
Magento是一套专业开源的PHP电子商务系统。Magento设计得非常灵活,具有模块化架构体系和丰富的功能。易于与第三方应用系统无缝集成。Magento开源网店系统的特点主要分以下几大类,网站管理促销和工具国际化支持SEO搜索引擎优化结账方式运输快递支付方式客户服务用户帐户目录管理目录浏览产品展示分析和报表Magento 1.6 主要包含以下新特性:•持久性购物 - 为不同的
[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(); ?>
相关推荐:










