通过代码实例说明如何化腐朽为优雅

简介: 一、背景 ​  最近我负责的活动促销系统中要在审批的时候增加计算参加活动的商品的毛利率的需求。但是我负责打辅助,主要是同事负责具体开发,我了解了他的实现方式思路以后,果断拒绝了,并给出了我的解决方案以及优点,他发现我的方案确实扩展性和可维护性更好以后就采用了,本文就来通过这个实例来说明如何让本腐朽的代码变得优雅起来。

一、背景

​  最近我负责的活动促销系统中要在审批的时候增加计算参加活动的商品的毛利率的需求。但是我负责打辅助,主要是同事负责具体开发,我了解了他的实现方式思路以后,果断拒绝了,并给出了我的解决方案以及优点,他发现我的方案确实扩展性和可维护性更好以后就采用了,本文就来通过这个实例来说明如何让本腐朽的代码变得优雅起来。

二、需求描述

​   活动系统中共有7中活动类型,分别为:价格折扣活动、满减活动、满赠活动、换购活动、满折活动、抢购活动、N元任选活动。每种活动类型都有自己的毛利率的计算方式。要求根据不同的活动类型来通过不同的计算方式计算参加活动的商品的毛利率。

三、开发运行环境

  1. Maven 3.3.9
  2. Spring 4.2.6.RELEASE
  3. JDK 1.7
  4. IDEA 15.04

四、同事方案1

  直接通过switch/case的方式判断不同的活动类型,然后每种类型给出不同的计算方式。

package com.hafiz.www.domain;

/**
 * @author hafiz.zhang
 * @description: 活动毛利率计算器
 * @date Created in 2017/11/28 20:52.
 */
public class Calculator {

    public static String calculate(Integer campaignType) {
        switch (campaignType) {
            case 1:
                return "价格折扣活动计算毛利率";
            case 2:
                return "满减活动计算毛利率";
            case 3:
                return "满赠活动计算毛利率";
            case 4:
                return "换购活动计算毛利率";
            case 5:
                return "满折活动计算毛利率";
            case 6:
                return "抢购活动计算毛利率";
            case 7:
                return "N元任选活动计算毛利率";
            default:
                return "错误的活动类型";
        }
    }
}

 

缺点:虽然写起来很简单,但是可扩展性差,或者说不具备可扩展性,若每种活动类型的计算毛利率方式都比较复杂,则Calculator类就会变得臃肿不堪。可维护性很差。完全就是面向过程的开发方式。被我一票拒绝。并告诉他通过定义接口,然后各种活动类型实现自己的计算方式,然后使用简单工厂模式通过Java的多态来实现。

五、同事方案2

定义计算接口,被针对每种活动给出不同的实现。

1.定义计算接口

package com.hafiz.www.handler;

import com.hafiz.www.enums.CampaignTypeEnum;

/**
 * @author hafiz.zhang
 * @description: 计算毛利率接口
 * @date Created in 2017/11/28 20:57.
 */
public interface ICampaignHandler {
    /**
     * 计算毛利率
     * @return
     */
    String calculate();
}

2.价格折扣活动实现

package com.hafiz.www.handler.impl;

import com.hafiz.www.enums.CampaignTypeEnum;
import com.hafiz.www.event.CampaignHandlerEvent;
import com.hafiz.www.handler.ICampaignHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: 价格折扣活动操作器
 * @author hafiz.zhang
 * @create 2017/11/28 20:52.
 */
public class PriceDiscountCampaignHandler implements ICampaignHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(PriceDiscountCampaignHandler.class);

    @Override
    public String calculate() {
        LOGGER.info("价格折扣活动计算毛利率");
        return "价格折扣活动计算毛利率";
    }
}

3.抢购类型活动实现

package com.hafiz.www.handler.impl;

import com.hafiz.www.enums.CampaignTypeEnum;
import com.hafiz.www.event.CampaignHandlerEvent;
import com.hafiz.www.handler.ICampaignHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: 抢购活动操作器
 * @author: hafiz.zhang
 * @create: 2017/11/28 20:52.
 */
public class PanicBuyCampaignHandler implements ICampaignHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(PanicBuyCampaignHandler.class);

    @Override
    public String calculate() {
        LOGGER.info("抢购活动计算毛利率");
        return "抢购活动计算毛利率";
    }
}

 

等等还有剩下各种活动自己的实现,此处为避免篇幅过长略去。

4.简单工厂

package com.hafiz.www.handler;

import com.hafiz.www.handler.impl.CheapenOtherCampaignHandler;
import com.hafiz.www.handler.impl.FullCutCampaignHandler;
import com.hafiz.www.handler.impl.FullDiscountCampaignHandler;
import com.hafiz.www.handler.impl.FullGIftCampaignHandler;
import com.hafiz.www.handler.impl.OptionCampaignHandler;
import com.hafiz.www.handler.impl.PanicBuyCampaignHandler;
import com.hafiz.www.handler.impl.PriceDiscountCampaignHandler;

/**
 * @author hafiz.zhang
 * @description: 操作器工厂类
 * @date Created in 2017/11/28 22:06.
 */
public class CampaignHandlerFactory {
    public static ICampaignHandler getHandler(Integer campaignType) {
        switch (campaignType) {
            case 1:
                return new PriceDiscountCampaignHandler();
            case 2:
                return new FullCutCampaignHandler();
            case 3:
                return new FullGIftCampaignHandler();
            case 4:
                return new CheapenOtherCampaignHandler();
            case 5:
                return new FullDiscountCampaignHandler();
            case 6:
                return new PanicBuyCampaignHandler();
            case 7:
                return new OptionCampaignHandler();
            default:
                throw new RuntimeException("错误的活动类型");
        }
    }
}

 

这样比第一版稍好一点,代码已经优雅了很多,可扩展性也好了很多,如果一旦增加新的活动类型,只需要新写一个新活动计算毛利率的操作器实现类就好了,然后再在工厂类中增加对应的case.但是还是没有很完美,这样需要每次都修改工厂类,不完美!

六、我的方案:使用Spring事件通知来实现简单工厂

1.接口定义

package com.hafiz.www.handler;

import com.hafiz.www.enums.CampaignTypeEnum;

/**
 * @author hafiz.zhang
 * @description: 计算毛利率接口
 * @date Created in 2017/11/28 20:57.
 */
public interface ICampaignHandler {

    CampaignTypeEnum getCampaignType();
    /**
     * 计算毛利率
     * @return
     */
    String calculate();
}

2.活动操作器自定义事件

package com.hafiz.www.event;

import com.hafiz.www.handler.ICampaignHandler;
import org.springframework.context.ApplicationEvent;

/**
 * @author hafiz.zhang
 * @description: 活动操作器事件
 * @date Created in 2017/11/28 21:02.
 */
public class CampaignHandlerEvent extends ApplicationEvent {

    public CampaignHandlerEvent(ICampaignHandler source) {
        super(source);
    }
}

2.价格折扣现类

package com.hafiz.www.handler.impl;

import com.hafiz.www.enums.CampaignTypeEnum;
import com.hafiz.www.event.CampaignHandlerEvent;
import com.hafiz.www.handler.ICampaignHandler;
import com.hafiz.www.spring.SpringAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Description: 价格折扣活动操作器
 * @author hafiz.zhang
 * @create 2017/11/28 20:52.
 */
@Component
public class PriceDiscountCampaignHandler implements ICampaignHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(PriceDiscountCampaignHandler.class);

    @PostConstruct
    public void init() {
        CampaignHandlerEvent event = new CampaignHandlerEvent(this);
        SpringAware.getApplicationContext().publishEvent(event);
    }

    @Override
    public CampaignTypeEnum getCampaignType() {
        return CampaignTypeEnum.PRICE_DISCOUNT;
    }

    @Override
    public String calculate() {
        LOGGER.info("价格折扣活动计算毛利率");
        return "价格折扣活动计算毛利率";
    }
}

3.抢购类活动实现类

package com.hafiz.www.handler.impl;

import com.hafiz.www.enums.CampaignTypeEnum;
import com.hafiz.www.event.CampaignHandlerEvent;
import com.hafiz.www.handler.ICampaignHandler;
import com.hafiz.www.spring.SpringAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Description: 抢购活动操作器
 * @author: hafiz.zhang
 * @create: 2017/11/28 20:52.
 */
@Component
public class PanicBuyCampaignHandler implements ICampaignHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(PanicBuyCampaignHandler.class);

    @PostConstruct
    public void init() {
        CampaignHandlerEvent event = new CampaignHandlerEvent(this);
        SpringAware.getApplicationContext().publishEvent(event);
    }

    @Override
    public CampaignTypeEnum getCampaignType() {
        return CampaignTypeEnum.PANIC_BUY;
    }

    @Override
    public String calculate() {
        LOGGER.info("抢购活动计算毛利率");
        return "抢购活动计算毛利率";
    }
}

还有另外几种活动类型的实现方式,为了避免篇幅过长不一一列举。

4.新工厂实现方式

package com.hafiz.www.handler;

import com.hafiz.www.enums.CampaignTypeEnum;
import com.hafiz.www.event.CampaignHandlerEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hafiz.zhang
 * @description: 活动操作器工厂类
 * @date Created in 2017/11/28 20:59.
 */
@Component
public class CampaignHandlerFactory implements ApplicationListener<CampaignHandlerEvent> {

    private static final Logger LOGGER = LoggerFactory.getLogger(CampaignHandlerFactory.class);

    private static Map<CampaignTypeEnum, ICampaignHandler> handlerMap = new ConcurrentHashMap<>();

    /**
     * 通过活动类型获取对应的操作器
     *
     * @param discountType 活动类型
     *
     * @return
     */
    public static ICampaignHandler getHandler(Integer discountType) {
        CampaignTypeEnum discountTypeEnum = CampaignTypeEnum.getEnumById(discountType);
        ICampaignHandler handler = handlerMap.get(discountTypeEnum);
        return handler;
    }

    /**
     * 注册绑定不同类型活动对应的活动操作器
     *
     * @param handler 活动操作器
     *
     */
    private void registerHandler(ICampaignHandler handler) {
        CampaignTypeEnum discountType = handler.getCampaignType();
        LOGGER.info("开始绑定{}类型的活动处理器", discountType.getName());
        handlerMap.put(discountType, handler);
    }

    @Override
    public void onApplicationEvent(CampaignHandlerEvent event) {
        ICampaignHandler handler = (ICampaignHandler) event.getSource();
        this.registerHandler(handler);
    }
}

说明 :新的工厂类中通过实现Spring的事件监听,接收到监听以后,直接获取事件源,保存在本地Map中,就很优雅。这样新增活动类型的时候工厂类完全不需要修改,而且现有类也不需要修改,只需要进行对新的活动类型扩展就好了。符合了软件开发中的开闭环原则。看起来很棒~

5.工具类SpringAware

package com.hafiz.www.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author hafiz.zhang
 * @description: spring 上下文工具类
 * @date Created in 2017/11/28 21:07.
 */
public class SpringAware implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    public SpringAware() {
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        applicationContext = ac;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    public static void rollBack() {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
}

 

其他Spring的配置以及测试用例等代码不再单独贴出,源码地址:https://github.com/hafizzhang/code-optimize.git

七、总结

​  在实际工作中,我们会碰到很多这种可以通过设计模式以及Java特性来实现优雅代码的机会,这个时候我们一定不能只为了省事写出烂代码,这样不但对自己的成长没有任何的好处,而且会对以后维护者造成很大困扰,我们要在保证工期和质量的前提下尽量的把代码写的优雅一点,尽量考虑到可扩展性以及可维护性等。这样才能在技术上有所提高,才能够自我成长。两全其美,何乐而不为呢?

​
相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16788 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
1天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1203 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2282 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3776 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。