外贸企业网站源码下载创新的成都网站建设
web/
2025/10/3 5:19:42/
文章来源:
外贸企业网站源码下载,创新的成都网站建设,WordPress搭建邮件服务器,做企业网站需要注意哪些为什么80%的码农都做不了架构师#xff1f; 1、公司项目需求。 用户签到活动#xff0c;会员签到怎么处理#xff0c;超级会员怎么处理#xff0c;普通用户签到怎么处理#xff0c;针对不同的档次#xff0c;有不同的方案#xff0c;所以在项目中用到了策… 为什么80%的码农都做不了架构师 1、公司项目需求。 用户签到活动会员签到怎么处理超级会员怎么处理普通用户签到怎么处理针对不同的档次有不同的方案所以在项目中用到了策略模式以及简单工厂模式。 其实生活中我们用到的软件系统都会有这样的制定级别比如我们是实体店他也有一个会员制度打9折打七折打六折的都有包括我们做头发也都有不同级别的剪发卡.... 这些理发店啊、服装店啊还有什么美容spa店等等用的软件都会涉及这就是我们所说的策略模式。 2、策略模式的概念 概念针对一类问题用不同的方式的解决这就是策略模式 举例1明星演唱会我们去买门票路人甲买的一等座路人乙买的二等座路人丙买的外围座虽然他们都能看到明星演唱但是位置不同针对买座位的不同级别来设置他们的观看明星位置就是策略模式 举例2去李宁专卖店买衣服路人甲和路人乙买同一件衣服虽然都能买到衣服但是路人甲是会员路人乙是普通用户他们支付的钱不一样路人甲花钱少针对买衣服不同人的级别折扣不一样就是策略模式 3、代码实现 项目中用到的比较多例如商城会员、签到级别、游戏上、这种项目用的策略设计模式比较多 说个场景服装店买衣服会员级别分别是AVip、BVip、CVip 和没有会员的用户NoVip四个级别打折情况分别是0.6、0.7、0.9和没有打折 打折的接口 public interface DisCount {//买东西给折扣Double disCount(Double money);
} 不同会员级别的实现类不同的折扣 package ceLve;public class AVip implements DisCount {Overridepublic Double disCount(Double money) {return money * 0.6 ;}
} package ceLve;public class BVip implements DisCount {Overridepublic Double disCount(Double money) {return money * 0.7;}
} package ceLve;public class CVip implements DisCount {Overridepublic Double disCount(Double money) {return money * 0.9;}
} package ceLve;public class NoVip implements DisCount {//普通用户不能打折Overridepublic Double disCount(Double money) {return money;}
} package ceLve;public class Customer {//客户消费总金额private Double totalAccount 0D;//客户目前消费金额private Double currentAccount 0D;//默认消费者 没有会员等级private DisCount disCount new NoVip();//消费者 消费商品、衣服的方法public void buy(Double money){this.currentAccount money;totalAccount money;if (totalAccount 10000){disCount new AVip();}else if (totalAccount 6000){disCount new BVip();}else if (totalAccount 3000){disCount new CVip();}else {disCount new NoVip();}}// 消费者支付public double pay(){return disCount.disCount(currentAccount);}public Double getTotalAccount() {return totalAccount;}public void setTotalAccount(Double totalAccount) {this.totalAccount totalAccount;}public Double getCurrentAccount() {return currentAccount;}public void setCurrentAccount(Double currentAccount) {this.currentAccount currentAccount;}} 最后在main函数中执行 package ceLve;public class Main {public static void main(String[] args){Customer customer new Customer();customer.buy(5000.00);System.out.println(customer.pay());}
} 运行结果4500.00 上面是纯粹的是策略模式现在我要加上简单工厂模式其实加上与不加上没有什么区别就自我感觉就是吃饭为什么非要用右手使用筷子而不是左手小时候家长我只要用左手就打我手。因为我们中国的传统就是右手所以因为传统的原因目前简单工厂设计模式也如此也基本没什么用但有人会说硬编码不能随随便便new那好吧今天把简单工厂模式也加进去。 修改方案修改Customer类新增简单工厂类CountFactory package ceLve;public class Customer {//客户消费总金额private Double totalAccount 0D;//客户目前消费金额private Double currentAccount 0D;//默认消费者 没有会员等级private DisCount disCount new NoVip();//消费者 消费商品、衣服的方法public void buy(Double money){this.currentAccount money;totalAccount money;disCount CountFactory.createCount(this);}// 消费者支付public double pay(){return disCount.disCount(currentAccount);}public Double getTotalAccount() {return totalAccount;}public void setTotalAccount(Double totalAccount) {this.totalAccount totalAccount;}public Double getCurrentAccount() {return currentAccount;}public void setCurrentAccount(Double currentAccount) {this.currentAccount currentAccount;}} public class CountFactory {private CountFactory(){}public static DisCount createCount(Customer customer){if (customer.getTotalAccount() 10000){return new AVip();}else if (customer.getTotalAccount() 6000){return new BVip();}else if (customer.getTotalAccount() 3000){return new CVip();}else {return new NoVip();}}
} main函数代码不变 结果依然4500.00 其实大家看下来就感觉没那么多必要的我直接if else也可以解决的没有这么麻烦呀不对的简单的小项目可以这样但是如果活动量增加的话没必要在controller或者service层去判断它应用那种策略他其实说白了就是一个处理方式的一个工具类我们把它封装好什么时候用什么时候调用就好就像jdk中的时间其实我们项目中也会有很多工具类他其实有点类似那样 转载于:https://my.oschina.net/mdxlcj/blog/1787501
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/86030.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!