博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 写策略模式,商场收银打折优惠策略
阅读量:4979 次
发布时间:2019-06-12

本文共 6501 字,大约阅读时间需要 21 分钟。

[Decode error - output not utf-8]-----------------------------  购物清单    方便面 : 100 x 50 = 5000 |  4000  菊花茶 : 10 x 50 = 500 |  500-----------------------------  优惠使用 : 国庆1折优惠  购物合计  4500 -> 450[Finished in 0.6s]

 

首先封装收银机类,怎么把商品设进,怎么把收银金额输出。

然后封装商品,和金额独立

然后进行收银策略编写。打折,满返针对的是最后的结果。

收银机添加设置策略接口,调用原生金额接口,调用策略接口,获得策略后金额接口

下个需求到商品的具体折扣,譬如买几送几

封装策略到商品处,商品创建的时候根据自己的名字到工厂去领取自己的“福利”

 

后续还想实施,组合折扣,譬如买牙膏同时买牙刷,减5块钱

架构要大改了(・-・*) ,暂时搁置 

/** * by JackChen 2016-3-15 19.20.01 * 看完马士兵老师策略模式后作业 * * 原文是讲解comparable接口 和 compareTo *  * 所有实现了comparable接口的类都可以用于比较。 *  * 而更高级的是,我不同场景需要不同的比较时,通过定制本类的比较器, * 借用比较器的compareTo,得到不同的比较结果 * * 已实现 * 1. 单个物品多个数量 * 2. 多个物品多个数量 * 3. 多个物品结算打印 * 4. 多个物品总金额折扣策略(几折、满返) * 5. 单个物品优惠策略(多少个送多少个) * * 待实现 * 6. 单个物品 买多个送多个 跟购物物品无关 * 7. 组合购买 *////  收银策略类//普通收钱var NormalStrategy = function() {    var self = this;    self.type = "total";    self.description = "没有使用优惠";};NormalStrategy.prototype = {};NormalStrategy.prototype.constructor = NormalStrategy;NormalStrategy.prototype.desc = function() {    return this.description;};NormalStrategy.prototype.discount = function(money) {    return money;};//折扣策略var PrecentOffStrategy = function(description, precent) {    var self = this;    self.type = "total";    self.precent = precent;    self.description = description + (precent*10) + "折优惠";};PrecentOffStrategy.prototype = new NormalStrategy();PrecentOffStrategy.prototype.constructor = PrecentOffStrategy;PrecentOffStrategy.prototype.desc = function() {    return this.description;};PrecentOffStrategy.prototype.discount = function(money) {    return money * this.precent;};//满返策略var GivebackStrategy = function(description, enough, giveback) {    var self = this;    self.type = "total";    self.enough = enough;    self.giveback = giveback;    self.description = description + "满"+ enough + "返" + giveback + "优惠";};GivebackStrategy.prototype = new NormalStrategy();GivebackStrategy.prototype.constructor = GivebackStrategy;GivebackStrategy.prototype.desc = function() {    return this.description;};GivebackStrategy.prototype.discount = function(money) {    if (money >= this.enough) {        money -= this.giveback;    };    return money;};///  销售品种折扣工厂var SaleItemStrategyFactory = function() {};SaleItemStrategyFactory.prototype = {};SaleItemStrategyFactory.prototype.constructor = SaleItemStrategyFactory;SaleItemStrategyFactory.prototype.getInstance = function(name) {    var self = this;    var strategy = null;    switch (name) {        case "方便面":            strategy = new BuyMoreStrategy("特惠",4,1);            break;        default:            // statements_def            break;    }    return strategy;};//普通var ItemNormalStrategy = function() {    var self = this;    self.type = "total";    self.description = "没有优惠";};ItemNormalStrategy.prototype = {};ItemNormalStrategy.prototype.constructor = ItemNormalStrategy;ItemNormalStrategy.prototype.desc = function() {    return this.description;};ItemNormalStrategy.prototype.discount = function(money) {    return money;};//买几送几var BuyMoreStrategy = function(description, buy, free) {    var self = this;    self.type = "total";    self.buy = buy;    self.free = free;    self.description = description + "买"+ buy + "送" + free;};BuyMoreStrategy.prototype = new ItemNormalStrategy();BuyMoreStrategy.prototype.constructor = BuyMoreStrategy;BuyMoreStrategy.prototype.desc = function() {    return this.description;};BuyMoreStrategy.prototype.discount = function(item) {    var give = item.num / (this.buy + this.free);    var left = item.num % (this.buy + this.free);    money = (give* this.buy + left)*item.price;    return money;};///  销售品种var SaleItem = function(name , price) {    var self = this;    self.name = name;    self.price = price;    self.num = 1;    self.strategy = factory.getInstance(self.name);};SaleItem.prototype = {};SaleItem.prototype.constructor = SaleItem;SaleItem.prototype.clone = function() {    var self = this;    var cloneItem = new SaleItem();    cloneItem.name = self.name;    cloneItem.price = self.price;    cloneItem.num = self.num;    cloneItem.strategy = self.strategy;    return cloneItem;};SaleItem.prototype.count = function() {    return this.price * this.num;};SaleItem.prototype.discountProcess = function(money) {    if (this.strategy) {        money = this.strategy.discount(this);    };    return money;};SaleItem.prototype.discount = function() {    return this.discountProcess(this.count());};///  收银策略类var CashRegister = function() {    var self = this;    self.totalDiscountStrategy = new NormalStrategy();    self.arr = [];};CashRegister.prototype = {};CashRegister.prototype.constructor = CashRegister;//添加商品CashRegister.prototype.add = function(item, num) {    var self = this;    if (num) {        item.num = num;    };        self.arr.push(item);};//添加折扣策略CashRegister.prototype.setTotalDiscountStrategy = function(strategy) {    this.totalDiscountStrategy = strategy;};//总计金额CashRegister.prototype.count = function() {    var self = this;    var totalMoney = 0;    self.arr.forEach( function(item, index) {        totalMoney += item.discount();    });    return totalMoney;};//折扣加入CashRegister.prototype.discountProcess = function(money) {    var self = this;    if (self.totalDiscountStrategy) {        money = self.totalDiscountStrategy.discount(money);    };    return money;};//折后金额CashRegister.prototype.discount = function() {    var self = this;    var totalMoney = self.count();    return self.discountProcess( totalMoney );};//结算清单CashRegister.prototype.print = function() {    var self = this;    console.log('-----------------------------');    console.log('  购物清单  ');    console.log('');    var totalMoney = 0;    self.arr.forEach(function(item, index) {        console.log("  %s : %s x %s = %s | ",item.name, item.price, item.num, item.count(),item.discount());    });    console.log('-----------------------------');    console.log('  优惠使用 : ' + self.totalDiscountStrategy.desc())    console.log('  购物合计  ' + self.count() +" -> "+ self.discount() );    console.log('');};///  测试类var factory = new SaleItemStrategyFactory();var cashRegister = new CashRegister();cashRegister.setTotalDiscountStrategy(new PrecentOffStrategy("国庆",0.1));// cashRegister.setTotalDiscountStrategy(new GivebackStrategy("劳动节",500,300));// cashRegister.setTotalDiscountStrategy(new GivebackStrategy("劳动节",1000,500));cashRegister.add(new SaleItem("方便面",100),50);cashRegister.add(new SaleItem("菊花茶",10),50);cashRegister.print();
View Code

 

转载于:https://www.cnblogs.com/Again/p/5284850.html

你可能感兴趣的文章
SQL常用语句大全
查看>>
LinkedList的使用方法
查看>>
SVN增量类
查看>>
bzoj5334: [Tjoi2018]数学计算
查看>>
bzoj4476: [Jsoi2015]送礼物
查看>>
ES6语法的学习与实践
查看>>
在express中HMR(合并express和webpack-dev-server)
查看>>
[Effective JavaScript 笔记]第55条:接收关键字参数的选项对象
查看>>
场景下开发-jq选择器
查看>>
make的工作过程
查看>>
unity3d questions!
查看>>
Base64图片编码原理,base64图片工具介绍,图片在线转换Base64
查看>>
表单验证
查看>>
hdu 1874 畅通工程续 Dijkstra
查看>>
hdu 2086
查看>>
浏览器兼容性问题
查看>>
Jsp页面里引入一个javascript文件,在jsp的onclick里怎么添加脚本文件里的方法
查看>>
用面向对象的方法重写选项卡
查看>>
三、 java运算符与流程控制
查看>>
bingil 组件LayerSelectControl lanix
查看>>