当前位置: 首页 > news >正文

社区门户网站建设方案/广州最新政策

社区门户网站建设方案,广州最新政策,建设工程中标查询,郑州市公司网站开发设计过多的if-else代码和嵌套,会使阅读代码的人很难理解到底是什么意思。尤其是那些没有注释的代码。其次是可维护性,因为if-else特别多,if-else是有办法可以消除掉的,其中比较典型的并且使用广泛的就是借助策略模式和工厂模式&#x…

过多的if-else代码和嵌套,会使阅读代码的人很难理解到底是什么意思。尤其是那些没有注释的代码。其次是可维护性,因为if-else特别多,if-else是有办法可以消除掉的,其中比较典型的并且使用广泛的就是借助策略模式和工厂模式,准确的说是利用这两个设计模式的思想,彻底消灭代码中的if-else。

传统用if else (伪代码)

public BigDecimal calPrice(BigDecimal orderPrice, String buyerType) {

if (用户是专属会员) {
if (订单金额大于30元) {
returen 7折价格;
}
}

if (用户是超级会员) {
return 8折价格;
}

if (用户是普通会员) {
if(该用户超级会员刚过期并且尚未使用过临时折扣){
临时折扣使用次数更新();
returen 8折价格;
}
return 9折价格;
}
return 原价;
}

策略模式

首先,定义一个接口:

/**

  • @author mhcoding
    */
    public interface UserPayService {

/**

  • 计算应付价格
    */
    public BigDecimal quote(BigDecimal orderPrice);
    }

接着定义几个策略类:

/**

  • @author mhcoding
    */
    public class ParticularlyVipPayService implements UserPayService {

@Override
public BigDecimal quote(BigDecimal orderPrice) {
if (消费金额大于30元) {
return 7折价格;
}
}
}

public class SuperVipPayService implements UserPayService {

@Override
public BigDecimal quote(BigDecimal orderPrice) {
return 8折价格;
}
}

public class VipPayService implements UserPayService {

@Override
public BigDecimal quote(BigDecimal orderPrice) {
if(该用户超级会员刚过期并且尚未使用过临时折扣){
临时折扣使用次数更新();
returen 8折价格;
}
return 9折价格;
}
}

利用工厂模式让代码自己判断使用策略

/**

  • @author mhcoding
    */
    public class UserPayServiceStrategyFactory {

private static Map<String,UserPayService> services = new ConcurrentHashMap<String,UserPayService>();

public static UserPayService getByUserType(String type){
return services.get(type);
}

public static void register(String userType,UserPayService userPayService){
Assert.notNull(userType,“userType can’t be null”);
services.put(userType,userPayService);
}
}

这个UserPayServiceStrategyFactory中定义了一个Map,用来保存所有的策略类的实例,并提供一个getByUserType方法,可以根据类型直接获取对应的类的实例。

有了这个工厂类之后,计算价格的代码即可得到大大的优化:

/**

  • @author mhcoding
    */
    public BigDecimal calPrice(BigDecimal orderPrice,User user) {

String vipType = user.getVipType();
UserPayService strategy = UserPayServiceStrategyFactory.getByUserType(vipType);
return strategy.quote(orderPrice);
}

最后我们利用spring调用register方法,把Spring通过IOC创建出来的Bean注册进去就行了。

这种需求,可以借用Spring种提供的InitializingBean接口,这个接口为Bean提供了属性初始化后的处理方法,它只包括afterPropertiesSet方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法。

将前面的各个策略类稍作改造:

/**

  • @author mhcoding
    */
    @Service
    public class ParticularlyVipPayService implements UserPayService,InitializingBean {

@Override
public BigDecimal quote(BigDecimal orderPrice) {
if (消费金额大于30元) {
return 7折价格;
}
}

@Override
public void afterPropertiesSet() throws Exception {
UserPayServiceStrategyFactory.register(“ParticularlyVip”,this);
}
}

@Service
public class SuperVipPayService implements UserPayService ,InitializingBean{

@Override
public BigDecimal quote(BigDecimal orderPrice) {
return 8折价格;
}

@Override
public void afterPropertiesSet() throws Exception {
UserPayServiceStrategyFactory.register(“SuperVip”,this);
}
}

@Service
public class VipPayService implements UserPayService,InitializingBean {

@Override
public BigDecimal quote(BigDecimal orderPrice) {
if(该用户超级会员刚过期并且尚未使用过临时折扣){
临时折扣使用次数更新();
returen 8折价格;
}
return 9折价格;
}

@Override
public void afterPropertiesSet() throws Exception {
UserPayServiceStrategyFactory.register(“Vip”,this);
}
}

只需要每一个策略服务的实现类都实现InitializingBean接口,并实现其afterPropertiesSet方法,在这个方法中调用UserPayServiceStrategyFactory.register即可。

这样,在Spring初始化的时候,当创建VipPayService、SuperVipPayService和ParticularlyVipPayService的时候,会在Bean的属性初始化之后,把这个Bean注册到UserPayServiceStrategyFactory中。

http://www.lbrq.cn/news/1556911.html

相关文章:

  • 网站内容该怎么做/各大网站收录入口
  • 网站建设构想/下载百度语音导航地图
  • 网站服务器搭建及配置的具体步骤/网络营销策略案例
  • 网站建设优化排名/sem工作原理
  • 阿拉伯语网站怎么做/seo电商运营是什么意思
  • 手机类网站设计/seo优化工作内容
  • 装修材料/百度seo排名优化技巧分享
  • 哪个网站做x展架比较好 知乎/semester是什么意思
  • 衢州网络公司做网站/抖音搜索seo排名优化
  • 湛江网站建设托管/公司网站设计方案
  • 东莞樟木头网站设计/软文500字范文
  • 做静态网站有什么建议/4a广告公司
  • 常用的做网站的工具都有哪些/企业站seo外包
  • 互动网站的核心技术/太原seo报价
  • dw建设网站步骤/网站seo优化案例
  • 建立健全安全生产责任制和安全生产规章制度/优化关键词有哪些方法
  • 十大牌子网/百度seo是啥
  • 做诱导网站/百度推广业务员
  • 东莞高端商城网站制作/如何销售自己产品方法有哪些
  • 网上做涉黄网站怎么判/谷歌浏览器2021最新版
  • 在线流程图网站怎么做/四川省人民政府
  • 建设银行网站 个人客户端/软件定制开发
  • 分析不同网站的优缺点/seopeixun com cn
  • 空间坐标系做图网站/优化大师win10下载
  • 无锡网站建设公司怎么样/建设一个网站的具体步骤
  • 专门做dm单的网站/郑州网站顾问热狗网
  • 陕西专业网站开发联系电话/小果seo实战培训课程
  • 学校网站建设情况说明书/百度推广助手手机版
  • 纪检委网站的建设的意义/百度站长号购买
  • 网络营销是营销的网络化吗/网站优化建设
  • YoloV9改进策略:Block改进-DCAFE,并行双坐标注意力机制,增强长程依赖与抗噪性-即插即用
  • C#WPF实战出真汁13--【营业查询】
  • STM32CUBEMX配置stm32工程
  • 软考 系统架构设计师系列知识点之杂项集萃(124)
  • Linux -- 文件【下】
  • c# WebAssembly,在网页上能运行多线程,异步,锁,原子加,减等代码吗