Memorydoc
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
    • HTML
    • CSS
    • 前端拓展
  • 编程之道

    • 并发编程
    • 设计模式
    • 数据结构算法
    • 技术拓展
    • 技术陷阱
    • 面试宝典
  • 分布式

    • 微服务
    • 数据库
  • 项目优化实战

    • JVM 优化
    • 线程池优化
    • 模板引擎优化
    • 任务调度优化
    • 内存优化
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Memorydoc

术尚可求
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
    • HTML
    • CSS
    • 前端拓展
  • 编程之道

    • 并发编程
    • 设计模式
    • 数据结构算法
    • 技术拓展
    • 技术陷阱
    • 面试宝典
  • 分布式

    • 微服务
    • 数据库
  • 项目优化实战

    • JVM 优化
    • 线程池优化
    • 模板引擎优化
    • 任务调度优化
    • 内存优化
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 并发编程

  • 设计模式

  • 数据结构算法

  • 技术拓展

    • 服务器搭建https
    • Maven 知识拓展
    • 项目集成Mybatis
    • 添加多个Ssh Key到一台电脑
    • BIO、NIO、AIO、Netty
    • 类加解密 & 自定义类加载器
    • 优化web 项目性能
    • 提升万倍系统吞吐量
    • 优雅写代码一
    • 优雅写代码二
      • 怎么更好的避免if else 语句?
        • 创建业务父类
        • 创建业务一代码
        • 创建业务二代码
        • 依赖Spring的 加载注入功能
        • 业务调用
      • 使用Spring 事件
        • 创建事件父类
        • 创建事件一
        • 创建事件二
        • 发布事件
      • 使用 @EventListener注解
        • 创建注解配置类
        • 创建事件类
        • 发布事件
    • 优雅写代码三
  • 技术陷阱

  • 面试宝典

  • 微服务

  • 数据库

  • 项目优化背景

  • JVM优化

  • 技术架构
  • 技术拓展
Memorydoc
2022-03-19

优雅写代码二原创

# 怎么更好的避免if else 语句?

# 创建业务父类

package com.sprucetec.live.category.bao;

import com.sprucetec.live.common.dto.baoreq.BaoReqDto;
import com.sprucetec.live.common.dto.baoreq.BaoUploadReqDto;
import com.sprucetec.live.common.dto.baores.BaoVideoResDto;
import lombok.Data;

/**
 * @ClassName BaoCallBack
 * @Description:
 * @Author Kenny
 * @Date 2020/6/10
 **/
@Data
public abstract class BaoCallBack {

    public String type; // 业务类型
    public abstract void handle(BaoUploadReqDto baoUploadReqDto);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 创建业务一代码

package com.sprucetec.live.category.bao;

import com.sprucetec.live.common.dto.baoreq.BaoUploadReqDto;
import com.sprucetec.live.common.dto.business.LiveVideoDto;
import com.sprucetec.live.entity.LiveCropVideo;
import com.sprucetec.live.entity.LiveVideo;
import com.sprucetec.live.enums.BaoResCodeEnum;
import com.sprucetec.live.enums.LiveStatusEnum;
import com.sprucetec.live.enums.SettingEnum;
import com.sprucetec.live.mapper.LiveCropVideoMapper;
import com.sprucetec.live.mapper.LiveVideoMapper;
import com.sprucetec.live.service.RedisService;
import lombok.Data;
import org.apache.coyote.http2.Setting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName VideoUpload
 * @Description:
 * @Author Kenny
 * @Date 2020/6/10
 **/

@Component
@Data
public class VideoUpload extends BaoCallBack {

    private String desc = "视频上传回调";
    public String type = "upload";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LiveVideoMapper liveVideoMapper;

    @Autowired
    private LiveCropVideoMapper liveCropVideoMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public void handle(BaoUploadReqDto baoVideoResDto) {
        String liveNo = null;
        Example liveExample = new Example(LiveVideo.class);
        Example cropVideoExample = new Example(LiveCropVideo.class);
        cropVideoExample.createCriteria().andEqualTo("vid", baoVideoResDto.getData().getVid());
        LiveCropVideo liveCropVideo = liveCropVideoMapper.selectOneByExample(cropVideoExample);
        if (liveCropVideo != null) {
            liveNo = liveCropVideo.getLiveNo();
        }
        liveExample.createCriteria().andEqualTo("liveNo", liveNo);
        LiveVideo liveVideo = new LiveVideo();
        if (BaoResCodeEnum.ERROR.getCode().equals(baoVideoResDto.getRet())) { // 失败
            liveVideo.setLiveStatus(LiveStatusEnum.UPLOADERROR.getCode());
        } else if (BaoResCodeEnum.SUCCESS.getCode().equals(baoVideoResDto.getRet())) {
            LiveCropVideo updateLiveCrop = new LiveCropVideo();
            updateLiveCrop.setIsUpload(SettingEnum.YES.getCode());
            liveCropVideoMapper.updateByExampleSelective(updateLiveCrop, cropVideoExample);
            liveVideo.setLiveStatus(LiveStatusEnum.UPLOADED.getCode());
        }
        liveVideo.setIsUpload(SettingEnum.YES.getCode()); // 设置直播上传状态 1成功 2失败
        liveVideo.setType(1); // 回看
        liveVideoMapper.updateByExampleSelective(liveVideo, liveExample);
        LiveVideoDto liveVideoDto = new LiveVideoDto(); // 清空缓存
        liveVideoDto.setLiveNo(liveNo);
        redisService.videoChangeRefreshCache(liveVideoDto);
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

# 创建业务二代码


import com.sprucetec.live.common.dto.baoreq.BaoUploadReqDto;
import com.sprucetec.live.common.dto.business.LiveVideoDto;
import com.sprucetec.live.entity.LiveCropVideo;
import com.sprucetec.live.entity.LiveVideo;
import com.sprucetec.live.enums.BaoResCodeEnum;
import com.sprucetec.live.enums.LiveStatusEnum;
import com.sprucetec.live.enums.SettingEnum;
import com.sprucetec.live.mapper.LiveCropVideoMapper;
import com.sprucetec.live.mapper.LiveVideoMapper;
import com.sprucetec.live.service.RedisService;
import lombok.Data;
import org.apache.coyote.http2.Setting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName VideoUpload
 * @Description:
 * @Author Kenny
 * @Date 2020/6/10
 **/

@Component
@Data
public class VideoUpload extends BaoCallBack {

    private String desc = "视频上传回调";
    public String type = "upload";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LiveVideoMapper liveVideoMapper;

    @Autowired
    private LiveCropVideoMapper liveCropVideoMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public void handle(BaoUploadReqDto baoVideoResDto) {
    
            //xxx  业务
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 依赖Spring的 加载注入功能

    Map<String, BaoCallBack> baoCallBackMap = new ConcurrentHashMap<>(); 
    /**
     * 利用Spring DI 将在项目重启,自动构建map,策略解决if else 问题
     * Spring 会在系统启动的过程中, 将实现了 BaoCallBack 的所有子类加载到 baoCallBackMap 集合中
     *
     * @param baoCallBacks
     */
    public LiveVideoThreeServiceImpl(List<BaoCallBack> baoCallBacks) {
        for (BaoCallBack baoCallBack : baoCallBacks) {
            baoCallBackMap.put(baoCallBack.getType(), baoCallBack);
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12

# 业务调用

// 通过类型直接从map中获取
 BaoCallBack baoCallBack = baoCallBackMap.get(CategoryEnum.UPLOAD.getCode());
 baoCallBack.handle(baoUploadReqDto);
1
2
3

# 使用Spring 事件

# 创建事件父类

package com.sprucetec.purchase.event;
import org.springframework.context.ApplicationEvent;

/**
 * @ClassName TestEventParent
 * @Description:
 * @Author Kenny
 * @Date 2020/9/29
 **/

public abstract class TestEventParent extends ApplicationEvent {
    public Integer businessType; // 业务类型
    /**
     * Constructs a prototypical Event.
     *
     * @param source The object on which the Event initially occurred.
     * @throws IllegalArgumentException if source is null.
     */
    public TestEventParent(Object source) {
        super(source);
    }

    public abstract  void execute();

    public Integer getBusinessType() {
        return businessType;
    }

    public void setBusinessType(Integer businessType) {
        this.businessType = businessType;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 创建事件一

package com.sprucetec.purchase.event;

import org.springframework.stereotype.Component;

/**
 * @ClassName TestEventOne
 * @Description:
 * @Author Kenny
 * @Date 2020/9/29
 **/
public class TestEventOne extends TestEventParent {
    /**
     * Constructs a prototypical Event.
     *
     * @param source The object on which the Event initially occurred.
     * @throws IllegalArgumentException if source is null.
     */
    public TestEventOne(Object source) {
        super(source);
    }

    public Integer getBusinessType() {
        return businessType;
    }

    public void setBusinessType(Integer businessType) {
        this.businessType = businessType;
    }

    private Integer businessType = 1;

    @Override
    public void execute() {
        System.out.println("执行TestEventOne" + businessType);
    }


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 创建事件二

package com.sprucetec.purchase.event;

import org.springframework.stereotype.Component;

/**
 * @ClassName TestEventOne
 * @Description:
 * @Author Kenny
 * @Date 2020/9/29
 **/
public class TestEventTwo extends TestEventParent {

    public Integer getBusinessType() {
        return businessType;
    }

    public void setBusinessType(Integer businessType) {
        this.businessType = businessType;
    }

    private Integer businessType = 1;

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public TestEventTwo(Object source) {
        super(source);
    }

    @Override
    public void execute() {
        System.out.println("执行TestEventTwo" + businessType);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 发布事件

// 注入spring 事件发布器
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

// 发布事件
     applicationEventPublisher.publishEvent(new TestEventOne(""));
1
2
3
4
5
6

# 使用 @EventListener注解

相当于使用 @EventListener 代替了事件监听器类

# 创建注解配置类

package com.sprucetec.purchase.config;

import com.sprucetec.purchase.event.TestEventOne;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @ClassName TestEventConfig
 * @Description:
 * @Author Kenny
 * @Date 2020/9/29
 **/
@Component
public class TestEventConfig {

    //@Async // 异步执行事件 默认为同步执行
    @EventListener
    public void handlerEvent(TestEventOne testEventOne){
        System.out.println("开始发送邮件");
        testEventOne.sendEmail();
        System.out.println("发送邮件结束");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 创建事件类

package com.sprucetec.purchase.event;

import org.springframework.context.ApplicationEvent;

/**
 * @ClassName TestEventOne
 * @Description:
 * @Author Kenny
 * @Date 2020/9/29
 **/
public class TestEventOne  extends ApplicationEvent {


    private String toUserName = "";

    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public TestEventOne(Object source) {
        super(source);
    }


    public  void sendEmail(){
        System.out.println("发送邮件" + toUserName);
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 发布事件

   TestEventOne testEventOne = new TestEventOne("");
        testEventOne.setToUserName("司泽刚");
        publisher.publishEvent(testEventOne);
        System.out.println("其他业务逻辑++++++++++++++++++");
        return ServiceResultUtil.success("DataImpot Success");
1
2
3
4
5
编辑 (opens new window)
上次更新: 2022/05/01, 19:42:49
优雅写代码一
优雅写代码三

← 优雅写代码一 优雅写代码三→

最近更新
01
命令模式 原创
05-03
02
桥接模式 原创
05-02
03
优雅写代码三 原创
04-29
更多文章>
Theme by Memorydoc | Copyright © 2021-2022 Memorydoc | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式