高级编程之结构化代码

背景:以下没结构化代码之前,定时器同步店铺信息的代码。

结构化的思想:SRP(单一职责),一个方法是做一件事,层次是相关的,逻辑和数据操作进行拆分,数据操作从业务流程上定义分层的步骤

结论:下面代码不规范,命名上不能见名知意,没有结构化代码,逻辑处理和数据库操作堆积到一起,还有实体的赋值,本身是实体的职责,却扔到了业务逻辑,所有东西堆积到一起,业务代码量少的时候还好,但是一旦业务代码量多,没有分层会造成难以理解逻辑,看代码的成本很大。

复制代码
@Service
public class SynVCStoreAppService {
    @Autowired
    private VCStoreClient vcStoreClient;
    @Autowired
    IMultiPlatformStoreInfoService iMultiPlatformStoreInfoService;

    /**
     * 同步店铺信息
     * 全量同步
     *
     * @return 结果
     */
    public Integer syncAll() {
    //调用客户端获取店铺信息
        LxErpMessage<List<VCStore>> lxErpMessage  = vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(0).length(200).build());
        List<VCStore> list =  lxErpMessage.getData();
        //判断是否存在数据
        if(lxErpMessage.getTotal()==0){
            return 0;
        }
        // 先查询数量
        int count = lxErpMessage.getTotal();

        // 每页查询数量
        int length = 200;
        List<VCStore> updataList = list;
        // 根据count 分批查询数据,如果是超过200则继续查询外部数据进行同步
        List<VCStore> list1 = null;
        if(count>200){
            for (int i = 200; i < count; i += length) {
                list1 = vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(i).length(length).build()).getData();
            }
            updataList.addAll(list1);
        }
        MultiPlatformStore mpsi = null;
        List<MultiPlatformStore> multiPlatformStoreList = new ArrayList<>();
        for (VCStore vcStore : updataList) {
            mpsi = new MultiPlatformStore();
            //因为是定时器,所以写死创建人信息,其他设置省略
            mpsi.setCreateId(1l);
            mpsi.setCreateBy("admin");
            mpsi.setCreateName("管理员");
            multiPlatformStoreList.add(mpsi);
        }
        iMultiPlatformStoreInfoService.saveOrUpdateBatch(multiPlatformStoreList);
        return count;
    }
}

重点:以下是结构化后的代码,只需要看syncAll()这个方法即可,其他都是分层处理,把逻辑处理和业务分离,以单一原则进行划分代码,同时也有了层次感,一目了然。在一层就是逻辑处理分配,在分批里调用具体的业务处理,业务里继续划分业务步骤进行数据处理

复制代码
@Slf4j
@Service
public class SynVCStoreAppService {
    private int length = 200;
    @Autowired
    private VCStoreClient vcStoreClient;
    @Autowired
    IMultiPlatformStoreInfoService iMultiPlatformStoreInfoService;

    /**
     * 同步店铺信息
     * 全量同步
     *
     * @return 结果
     */
    public Integer syncAll() {
        log.info("同步店铺--开始");
        int offset = 0;
        int total = doExcute(offset);
        while (total >this.length) {
            offset += this.length;
            doExcute(offset);
            total -= this.length;
        }
        log.info("同步店铺--结束");
        return total;
    }
    //分批获取店铺信息
    private int doExcute(int offset) {
        LxErpMessage<List<VCStoreVO>> erpVCStoreResponse  = getErpVCStore(offset);
        List<MultiPlatformStore> multiPlatformStores =  getMultiPlatformStoreByErpVCStoreResponse(erpVCStoreResponse);
        log.info("multiPlatformStores:{}",multiPlatformStores);
        iMultiPlatformStoreInfoService.saveOrUpdateBatch(multiPlatformStores);
        return erpVCStoreResponse.getTotal();
    }
	//调用第三方接口
    LxErpMessage<List<VCStoreVO>> getErpVCStore(int offset){
        return vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(offset).length(this.length).build());
    }
//处理返回的数据,这里存在代码严谨性,直接根据返回code再进行处理数据,而不是直接处理
    List<MultiPlatformStore> getMultiPlatformStoreByErpVCStoreResponse(LxErpMessage<List<VCStoreVO>> erpVCStoreResponse){
        List<MultiPlatformStore> multiPlatformStores =  new ArrayList<>();
        if(erpVCStoreResponse.isSuccess()&& !CollectionUtils.isEmpty(erpVCStoreResponse.getData())){
            erpVCStoreResponse.getData().forEach(e->{
               multiPlatformStores.add(e.getMultiPlatformStoreByVCStore());
            });
        }
        return multiPlatformStores;
    }
}

那些赋值的for循环放到实体里进行,创建实体的时候直接调用

复制代码
public class VCStoreVO {
public MultiPlatformStore getMultiPlatformStoreByVCStore(){
        MultiPlatformStore multiPlatformStore = new MultiPlatformStore();
        multiPlatformStore.setCreateBy("admin");
        multiPlatformStore.setCreateId(1L);
        multiPlatformStore.setCreateBy("admin");
        //其他省略
        return multiPlatformStore;
    }
  }

结构化后的代码,有了层次感,有问题直接从调用方法的里直接查阅,而不是需要从头看到尾去理解完代码才知道做了什么事情

相关推荐
indexsunny5 小时前
互联网大厂Java求职面试实战:Spring Boot微服务与Redis缓存场景解析
java·spring boot·redis·缓存·微服务·消息队列·电商
无心水5 小时前
【分布式利器:腾讯TSF】7、TSF高级部署策略全解析:蓝绿/灰度发布落地+Jenkins CI/CD集成(Java微服务实战)
java·人工智能·分布式·ci/cd·微服务·jenkins·腾讯tsf
28岁青春痘老男孩10 小时前
JDK8+SpringBoot2.x 升级 JDK 17 + Spring Boot 3.x
java·spring boot
方璧10 小时前
限流的算法
java·开发语言
元Y亨H10 小时前
Nacos - 服务注册
java·微服务
曲莫终10 小时前
Java VarHandle全面详解:从入门到精通
java·开发语言
独自归家的兔10 小时前
Spring Cloud核心架构组件深度解析(原理+实战+面试高频)
spring cloud·面试·架构
天若有情67310 小时前
校园二手交易系统实战开发全记录(vue+SpringBoot+MySQL)
vue.js·spring boot·mysql
一心赚狗粮的宇叔10 小时前
中级软件开发工程师2025年度总结
java·大数据·oracle·c#
while(1){yan}11 小时前
MyBatis Generator
数据库·spring boot·java-ee·mybatis