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)
  • 并发编程

  • 设计模式

    • 适配器模式(类兼容、适配、功能合并)
    • 装饰器模式(功能扩展、可以解决继承弊端)
    • 委派模式(分配任务,统一管理)
    • 简单工厂模式
    • 抽象工厂(解耦工厂)
    • Java自带的观察者模式(监听观察)
    • 实现观察者模式(监听观察)
    • 原型模式(克隆对象,深度复制、浅度复制)
    • 策略模式
    • 模板模式(自定义框架,具体方法用户自己实现)
    • 单例模式
    • CGlib(方法增强)
    • JDK动态代理(方法增强)
    • 自己实现静态代理
    • 处理链模式一
    • 处理链模式二
    • 处理链模式三
      • 背景
      • 责任链业务
      • 辅助实体
        • 抽象责任链
        • 抽象责任链
      • INFO 责任链
      • ERROR责任链
        • 用户登陆服务
        • 责任链构建器
    • 事件模式一
    • 事件模式二
    • 回调模式
    • 建造者模式
    • 桥接模式
    • 命令模式
  • 数据结构算法

  • 技术拓展

  • 技术陷阱

  • 面试宝典

  • 微服务

  • 数据库

  • 项目优化背景

  • JVM优化

  • 技术架构
  • 设计模式
Memorydoc
2022-03-18

处理链模式三原创

# 背景

日志级别分为 info、debug、error 那么如何通过责任链模式实现一套 控制日志打印级别的程序呢?

# 责任链业务

@Data
@AllArgsConstructor
public class ChainBusiness {

    public int level;// 处理级别

    public String businessType;// 处理时间类型

}

1
2
3
4
5
6
7
8
9
10

# 辅助实体

public class Constant {
    public static class ChainEnum {
        public static final int INFO = 1;//一级处理级别
        public static final int DEBUG = 2;//二级处理级别
        public static final int ERROR = 3;//三级处理级别
    }
}

1
2
3
4
5
6
7
8

# 抽象责任链

public abstract class Handler {

    protected int level;//默认一级处理级别

    public Handler(int level) {
        this.level = level;
    }

    private Handler nextHandler;//下一个处理器

    //获取下一个责任链
    public Handler getNextHandler() {
        return nextHandler;
    }

    //设置下一个责任链
    public void setNextHandler(Handler handler) {
        this.nextHandler = handler;
    }

    abstract protected void handle(ChainBusiness chainBusiness);//真正的处理逻辑在父类中不做处理,在子类中做处理

    //处理功能,判断是否存在处理链,如果存在则交由下一个处理链处理
    ///传入处理级别,设计责任链逻辑
    public void execute(ChainBusiness chainBusiness) {
        if (this.level <= chainBusiness.getLevel()) {//如果处理级别大于当前处理器的处理级别,那么全部处理
            //这里是不同的处理器的处理方法,
            //execute方法是公共的设计方法,具体的处理逻辑在每个处理器当中
            this.handle(chainBusiness);
        }
        if (nextHandler != null) {
            nextHandler.execute(chainBusiness);
        }
    }
}
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

# 抽象责任链


/**
 * @program: spring-test
 * @ClassName AbstractHandler
 * @description: 抽象责任链处理器
 * * 1. 参数校验 2. 用户名密码校验 3. 权限校验
 * @author: sizegang
 * @create: 2022-01-01 00:41
 * @Version 1.0
 **/
public abstract class AbstractLoginExecutor<T> implements LoginChain {

    protected Chain chain;

    public void next(Chain chain) {
        this.chain = chain;
    }

    // 这里结合模板方法
    @Override
    public void execute(Member member) {
        doExecute(member);
    }
    /**
     * 责任链模式配合建造者模式
     *
     * @param <T>
     */
    public static class Builder<T> {
        private AbstractLoginExecutor<T> head;
        private AbstractLoginExecutor<T> tail;

        public Builder<T> addChain(AbstractLoginExecutor<T> handler) {
            if (this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            // 设置自身的下一个节点
            this.tail.next(handler);
            // 修改指针指向
            this.tail = handler;
            return this;
        }

        public AbstractLoginExecutor<T> build() {
            return this.head;
        }

    }
}
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

# INFO 责任链

@Slf4j
public class InfoHandler extends Handler {
    public InfoHandler(int level) {
        super(level);
    }

    @Override
    protected void handle(ChainBusiness chainBusiness) {
        log.info("我是info处理器,处理{}", chainBusiness.getBusinessType());
    }
}
1
2
3
4
5
6
7
8
9
10
11

### DEBUG 责任链
```java 
@Slf4j
public class DebuggerHandler extends Handler {
    public DebuggerHandler(int level) {
        super(level);
    }

    @Override
    protected void handle(ChainBusiness chainBusiness) {
        log.info("我是debugger处理器, 处理{}", chainBusiness.getBusinessType());
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# ERROR责任链

@Slf4j
public class ErrorHandler extends Handler {
    public ErrorHandler(int level) {
        super(level);
    }
    @Override
    protected void handle(ChainBusiness chainBusiness) {
        log.info("我是error处理器, 处理{}", chainBusiness.getBusinessType());
    }
}

1
2
3
4
5
6
7
8
9
10
11

# 用户登陆服务

public class LoginAssembler {
    /**
     * 会员等六
     * @param member 会员
     */
    public void login(Member member){
        AbstractLoginExecutor.Builder builder = new AbstractLoginExecutor.Builder();
        AbstractLoginExecutor build = builder.addChain(new ValidateExecutor()).addChain(new LoginExecutor()).addChain(new AuthHandler()).build();
        build.execute(member);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 责任链构建器

public class RuleHandler {
    public static Handler initChain() {
        InfoHandler infoHandler = new InfoHandler(Constant.ChainEnum.INFO);
        DebuggerHandler debuggerHandler = new DebuggerHandler(Constant.ChainEnum.DEBUG);
        ErrorHandler errorHandler = new ErrorHandler(Constant.ChainEnum.ERROR);
        infoHandler.setNextHandler(debuggerHandler);
        debuggerHandler.setNextHandler(errorHandler);
        return infoHandler;
    }
}

## 测试
```java  
public class Test {
    public static void main(String[] args) {
        Handler handler = RuleHandler.initChain();//初始化处理链
        /*handler.execute(1, "哈哈哈哈");处理链一,只执行处理链一的内容*/
        ChainBusiness chainBusiness = new ChainBusiness(Constant.ChainEnum.DEBUG, "debugger");
        handler.execute(chainBusiness);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编辑 (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
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式