1. <?php
    //策略模式就是你有很多的方法,选择一种适合自己的,
    // 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,
    // 工厂模式就是
  2.  
  3. //策略模式 优惠系统、工资计算系统
    //工厂模式 主要应用在多数据库选择,类库文件加载等
  4.  
  5. //商场收银系统有正常收费,打折收费,返利收费模式
    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;
    }
    }
  6.  
  7. // 加载所有的策略
  8.  
  9. // 创建一个环境类,根据不同的需求调用不同策略
    class Factory{
    private $_strategy = null;
    public function __construct($type = null){
    if(!isset($type))
    return;
    $this->setCashStrategy($type);
  10.  
  11. }
    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);
    }
  12.  
  13. }
  14.  
  15. /*
    * 客户端类
    * 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合,
    * 使业务逻辑的算法更具有可移植性
    */
    class Client{
  16.  
  17. public function main(){
    $total = 0;
  18.  
  19. $factory = new Factory();
  20.  
  21. // 购买数量
    $numA = 10;
    // 单价
    $priceA = 100;
    // 策略模式获取结果
    $totalA = $factory->getResultAll('normal', $numA, $priceA);
    $this->display('A', 'normal', $numA, $priceA, $totalA);
  22.  
  23. // 购买数量
    $numB = 5;
    // 单价
    $priceB = 100;
    // 打折策略获取结果
    $totalB = $factory->getResultAll('rebate8', $numB, $priceB);
    $this->display('B', 'rebate8', $numB, $priceB, $totalB);
  24.  
  25. // 购买数量
    $numC = 10;
    // 单价
    $priceC = 100;
    // 返利策略获取结果
    $totalC = $factory->getResultAll('return300to100', $numC, $priceC);
    $this->display('C', 'return300to100', $numC, $priceC, $totalC);
    }
  26.  
  27. /**
    * 打印
    *
    * @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>";
    }
    }
  28.  
  29. /**
    * 程序入口
    */
    function start(){
    $client = new Client();
    $client->main();
    }
  30.  
  31. start();
  32.  
  33. ?>
  34.  
  35. 打印结果:
    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. dart实例

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  3. .NET BackgroundWorker的一般使用方式

    代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  4. POJ 2594 Treasure Exploration(最小可相交路径覆盖)题解

    题意:有n个点,m条单向边,每个机器人能沿着单向边走,能重复经过一个点,问最少几个机器人走遍n个点 思路:原来以前学的都是不能相交的算法....可相交的做法是跑Floyd把能到达的都加上边,然后跑最小 ...

  5. 【分布式事务】spring cloud集成lcn解决分布式事务

    参考地址:https://blog.csdn.net/u010882691/article/details/82256587 参考地址:https://blog.csdn.net/oyh1203/ar ...

  6. ThreadLocal使用

    ThreadLocal提供了一种访问某个变量的特殊方式:访问到的变量属于当前线程,即保证每个线程的变量不一样,而同一个线程在任何地方拿到的变量都是一致的,这就是所谓的线程隔离. 如果要使用Thread ...

  7. 良品铺子:“新零售”先锋的IT必经之路

    良品铺子:“新零售”先锋的IT必经之路 云计算 大数据 CIO班 CIO 互联网+ 物联网 电子政务 2017-12-29 09:25:34  来源:互联网抢沙发 摘要:2017年被称为“新零售”元年 ...

  8. R语言可视化学习笔记之添加p-value和显著性标记--转载

    https://www.jianshu.com/p/b7274afff14f?from=timeline #先加载包 library(ggpubr) #加载数据集ToothGrowth data(&q ...

  9. 微信小游戏开发之JS面向对象

    //游戏开发之面向对象 //在js的开发模式中有两种模式:函数式+面向对象 //1.es5 // 拓展一:函数的申明和表达式之间的区别 // 函数的申明: // function funA(){ // ...

  10. ERR! registry error parsing json

    报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...