php 商场收银收费系统,使用的策略模式
<?php
//策略模式就是你有很多的方法,选择一种适合自己的,
// 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,
// 工厂模式就是 //策略模式 优惠系统、工资计算系统
//工厂模式 主要应用在多数据库选择,类库文件加载等 //商场收银系统有正常收费,打折收费,返利收费模式
interface cashStrategy{
public function acceptCash($money);
}
//正常收费
class NormalStrategy implements cashStrategy{
public function acceptCash($money){
return $money;
}
}
//打折收费
class RebateStrategy implements cashStrategy{
//打折比例
private $moneyRebate = 1;
public function __construct($rebate){
$this->moneyRebate = $rebate;
}
public function acceptCash($money){
return $this->moneyRebate*$money;
}
}
//返利收费
class ReturnStrategy implements cashStrategy{
//返利条件
private $moneyCondition = null;
//返利多少
private $moneyReturn = null;
public function __construct($moneyCondition ,$moneyReturn){
$this->moneyCondition = $moneyCondition;
$this->moneyReturn = $moneyReturn;
}
public function acceptCash($money){
if(!isset($this->moneyCondition) || !isset($this->moneyReturn) || $this->moneyCondition == 0)
return $money;
return $money - floor($money/$this->moneyCondition)*$this->moneyReturn;
}
} // 加载所有的策略 // 创建一个环境类,根据不同的需求调用不同策略
class Factory{
private $_strategy = null;
public function __construct($type = null){
if(!isset($type))
return;
$this->setCashStrategy($type); }
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;
}
//获取结果
public function getResult($money){
return $this->_strategy->acceptCash($money);
}
public function getResultAll($type, $num, $price){
$this->setCashStrategy($type);
return $this->getResult($num * $price);
} } /*
* 客户端类
* 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合,
* 使业务逻辑的算法更具有可移植性
*/
class Client{ public function main(){
$total = 0; $factory = new Factory(); // 购买数量
$numA = 10;
// 单价
$priceA = 100;
// 策略模式获取结果
$totalA = $factory->getResultAll('normal', $numA, $priceA);
$this->display('A', 'normal', $numA, $priceA, $totalA); // 购买数量
$numB = 5;
// 单价
$priceB = 100;
// 打折策略获取结果
$totalB = $factory->getResultAll('rebate8', $numB, $priceB);
$this->display('B', 'rebate8', $numB, $priceB, $totalB); // 购买数量
$numC = 10;
// 单价
$priceC = 100;
// 返利策略获取结果
$totalC = $factory->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<br>";
}
} /**
* 程序入口
*/
function start(){
$client = new Client();
$client->main();
} start(); ?> 打印结果:
2018-10-09 11:17:30,A,[normal],num:10,price:100,total:1000
2018-10-09 11:17:30,B,[rebate8],num:5,price:100,total:400
2018-10-09 11:17:30,C,[return300to100],num:10,price:100,total:700
php 商场收银收费系统,使用的策略模式的更多相关文章
- 读《大话设计模式》——应用工厂模式的"商场收银系统"(WinForm)
要做的是一个商场收银软件,营业员根据客户购买商品单价和数量,向客户收费.两个文本框,输入单价和数量,再用个列表框来记录商品的合计,最终用一个按钮来算出总额就可以了,还需要一个重置按钮来重新开始. 核心 ...
- [Python设计模式] 第2章 商场收银软件——策略模式
github地址: https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序, 模拟商场收银软件,根据客户购买商品的单价和数量,计 ...
- 读《大话设计模式》——应用策略模式的"商场收银系统"(WinForm)
策略模式的结构 这个模式涉及到三个角色: 环境(Context)角色:持有一个 Strategy 类的引用.抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现.此角色给出所 ...
- 菜鸟学开店—最简收银POS系统
佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...
- javascript 写策略模式,商场收银打折优惠策略
[Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...
- C++ 大作业 超市收银系统
#include<iostream> #include<fstream> #include<string> #include<iomanip> #inc ...
- PDA 收银系统PDA手持打印扫描枪 销售开单 收银 扫描打印一体机
在零售方面也有很好的应用.如在一些高端品牌零售店,营业员可以随身导购,一站式完成了商品销售和收银,很是受消费者追捧,符合了企业对客户体验以及行业领先的追求. PDA收银系统是一款多功能可以取代专业收银 ...
- 浩瀚移动POS收银开单扫描解决方案PDA仓储系统,无线批发,移动批发,无线POS,无线销售APP-车销管理PDA
适用范围 各种业态的批发商铺.批发市场.订货会.展销会.配送中心仓库…… 产品简介 随着移动技术与智能PDA设备的迅猛发展,中国已经跨步进入移动信息化社会.移动商务是移动信息社会的重要载体与形式,它开 ...
- Atitit.收银系统pos 以及打印功能的行业标准
Atitit.收银系统pos 以及打印功能的行业标准 1. ESC指令序列 Escape指令序列不同于ESC/POS指令 1 2. 打印标准OPOS POSPrinter 与 CashDrawer 驱 ...
随机推荐
- Python常用库之Pilow
基本用法 静态方法 PIL.Image.open(fp, mode=’r’) 传入文件路径(str),返回一个image对象 PIL.Image.alpha_composite(im1, im2) 混 ...
- 展讯7731C_M Android6.0 充电指示灯实现(一)------关机充电实现【转】
本文转载自:https://blog.csdn.net/m0_37870649/article/details/80566131 前言: 在手机充电中常常使用充电指示灯来观察手机充电状态,比如说将手机 ...
- P4450 双亲数
思路 同zap-queries 莫比乌斯反演的板子 数据范围小到不用整除分块... 代码 #include <cstdio> #include <algorithm> #inc ...
- (转)Is attacking machine learning easier than defending it?
转自:http://www.cleverhans.io/security/privacy/ml/2017/02/15/why-attacking-machine-learning-is-easier- ...
- mybatis配置文件namespace用法总结
本文为博主原创,未经允许不得转载: 由于在应用过程中,发现namespace在配置文件中的重要性,以及配置的影响,在网上看了很多博客,发现很多人对namespace存在误解, 所以总结一下namesp ...
- BZOJ 1070: [SCOI2007]修车(费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1070 题意: 思路: 神奇的构图. 因为排在后面的人需要等待前面的车修好,这里将每个技术人员拆成n个 ...
- go 一波走起
$ go run helloworld.go 运行 $ go build helloworld.go 编译 该命令生成一个名为helloworld的可执行的二进制文件,可以随时运行它 $ ./hell ...
- mariaDB中文乱码
cetos7 下 http://hongjun.blog.51cto.com/445761/400985 1 . copy 一个文件成 /etc/my.cnf cp /usr/share/mysql ...
- Adobe photoshop CS5(32位and64位)破解补丁
转载自:http://www.wusiwei.com 网上有很多photoshop cs5的永久序列号,但这个在2年前还能有用,现在一般都不行,序列号给你验证过了,然后过几秒钟还是会弹出要你输入序列号 ...
- Web阶段总结以及感受(附带大一结束暑期学习的纲要)
之前本人大一因为不是计算机专业的,而又喜欢计算机,所以在大一临时转专业到了计算机院(费劲一番波折),冷笑,还好,从大二开始就可以正式学习喜欢的软件了. 首先,前两天看到一个讲座,提到学习方法,并说出总 ...