<?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 商场收银收费系统,使用的策略模式的更多相关文章

  1. 读《大话设计模式》——应用工厂模式的"商场收银系统"(WinForm)

    要做的是一个商场收银软件,营业员根据客户购买商品单价和数量,向客户收费.两个文本框,输入单价和数量,再用个列表框来记录商品的合计,最终用一个按钮来算出总额就可以了,还需要一个重置按钮来重新开始. 核心 ...

  2. [Python设计模式] 第2章 商场收银软件——策略模式

    github地址: https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序, 模拟商场收银软件,根据客户购买商品的单价和数量,计 ...

  3. 读《大话设计模式》——应用策略模式的"商场收银系统"(WinForm)

    策略模式的结构 这个模式涉及到三个角色: 环境(Context)角色:持有一个 Strategy 类的引用.抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现.此角色给出所 ...

  4. 菜鸟学开店—最简收银POS系统

    佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...

  5. javascript 写策略模式,商场收银打折优惠策略

    [Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...

  6. C++ 大作业 超市收银系统

    #include<iostream> #include<fstream> #include<string> #include<iomanip> #inc ...

  7. PDA 收银系统PDA手持打印扫描枪 销售开单 收银 扫描打印一体机

    在零售方面也有很好的应用.如在一些高端品牌零售店,营业员可以随身导购,一站式完成了商品销售和收银,很是受消费者追捧,符合了企业对客户体验以及行业领先的追求. PDA收银系统是一款多功能可以取代专业收银 ...

  8. 浩瀚移动POS收银开单扫描解决方案PDA仓储系统,无线批发,移动批发,无线POS,无线销售APP-车销管理PDA

    适用范围 各种业态的批发商铺.批发市场.订货会.展销会.配送中心仓库…… 产品简介 随着移动技术与智能PDA设备的迅猛发展,中国已经跨步进入移动信息化社会.移动商务是移动信息社会的重要载体与形式,它开 ...

  9. Atitit.收银系统pos 以及打印功能的行业标准

    Atitit.收银系统pos 以及打印功能的行业标准 1. ESC指令序列 Escape指令序列不同于ESC/POS指令 1 2. 打印标准OPOS POSPrinter 与 CashDrawer 驱 ...

随机推荐

  1. selinux权限问题【转】

    本文转载自:https://blog.csdn.net/u011386173/article/details/83339770 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  2. WSDL(Web服务描述语言)详细解析(全文转载学习用)

    WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务 ...

  3. 黄金连分数|2013年蓝桥杯B组题解析第四题-fishers

    黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜,它首次 ...

  4. 深度学习课程笔记(十一)初探 Capsule Network

    深度学习课程笔记(十一)初探 Capsule Network  2018-02-01  15:58:52 一.先列出几个不错的 reference: 1. https://medium.com/ai% ...

  5. Windows10状态栏右下角的上升三角号没有了

    闲着没事装了360和电脑管家,捣鼓了下里面的功能,好像是弄了桌面整理和主题之后出现了这个问题,刚开始还以为因为清理卸载软件把系统的这项功能给卸载了,其实原因很简单,相信看完解决这个问题的方案你就明白了 ...

  6. 并发学习一、MPI初步认识

    学习参考地址:https://www.jianshu.com/p/2fd31665e816 编程使用的vs2015 社区版本(个人感觉比Vc6.0的丑界面看起来舒服多了) MPI基本函数 MPI调用借 ...

  7. Docker:Swarms

    Prerequisites Install Docker version 1.13 or higher. Get Docker Compose as described in Part 3 prere ...

  8. RabbitMq的整理 exchange、route、queue关系

    https://blog.csdn.net/samxx8/article/details/47417133

  9. HDU 5452 Minimum Cut(LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=5452 题意: 有一个连通的图G,先给出图中的一棵生成树,然后接着给出图中剩余的边,现在要删除最少的边使得G不连通 ...

  10. 51nod 1689 逛街(优先队列)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1689 题意: 题意: 枚举终点,这样就确定路上的花费,接下来只需要计算进 ...