设计模式: 责任链模式

目录

一,责任链模式

二,特点

四,实现步骤

五,代码


一,责任链模式

责任链模式(Chain of Responsibility Pattern)是一种软件设计模式,它属于行为型模式。在这种模式中,一个请求沿着一个由多个处理对象组成的链顺序传递,直到链上的某个处理对象能够处理该请求为止。每个处理对象决定是处理该请求、拒绝它,还是将其传递给链中的下一个处理对象。

二,特点

  1. 解耦:将请求的发送者和接收者解耦,使得发送者不需要知道谁是请求的处理者,以及链的结构如何。

  2. 动态性:在运行时,可以动态地改变链的结构,例如增加或删除链中的某些处理对象。

  3. 避免请求发送者与接收者之间的耦合:请求发送者只需要知道链的头部,而不需要了解链的其余部分。

  4. 易于扩展:可以根据需要添加新的请求处理对象,而不需要修改现有代码

三,组成部分

  • Handler:定义了一个处理请求的接口,通常包含一个指向下一个处理者的引用。
  • ConcreteHandler :实现了Handler接口的具体处理类,决定是否处理请求或将请求传递给下一个处理者。

四,实现步骤

  1. 定义Handler接口,包含setNext()方法用于设置链的下一个处理者,以及handleRequest()方法用于处理请求。
  2. 创建具体的处理者类,实现Handler接口,并在handleRequest()方法中实现具体的请求处理逻辑。
  3. 将处理者对象通过setNext()方法连接起来,形成一条处理链。
  4. 发送请求,从链的开始传递,直到被处理或传递到链的末尾。

五,代码

1.创建抽象的记录器类。

java 复制代码
public abstract class AbstractLogger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;
    protected int level;
    //下一个元素
    AbstractLogger nextLogger;

    protected void nextLogger(AbstractLogger nextLogger) {
        this.nextLogger = nextLogger;
    }

    public void logMessage(int level, String message) {
        if (this.level <= level) {//如果小于传入的,那么打印出来
            write(message);
        }
        if (nextLogger != null) {
            nextLogger.logMessage(level, message);//下一个
        }
    }

    abstract protected void write(String message);
}

2.创建扩展了该记录器类的实体类。

java 复制代码
public class ConsoleLogger extends AbstractLogger{//控制台日志
    public ConsoleLogger(int level){
        this.level = level;
    }
    @Override
    protected void write(String message) {
        System.out.println("Standard Console::logger" + message);
    }
}
java 复制代码
public class ErrorLogger extends AbstractLogger{
    public ErrorLogger(int level){
        this.level = level;
    }
    @Override
    protected void write(String message) {
        System.out.println("Error Console::logger" + message);
    }
}
java 复制代码
public class FileLogger extends AbstractLogger{
    public FileLogger(int level){
        this.level = level;
    }
    @Override
    protected void write(String message) {
        System.out.println("File Console::logger" + message);
    }
}

3.创建不同类型的记录器。赋予它们不同的错误级别,并在每个记录器中设置下一个记录器。每个记录器中的下一个记录器代表的是链的一部分。

java 复制代码
public class ChainPatternDemo {
    public static AbstractLogger getChains(){
        AbstractLogger error = new ErrorLogger(AbstractLogger.ERROR);//3
        AbstractLogger file = new FileLogger(AbstractLogger.DEBUG);//2
        AbstractLogger console = new ConsoleLogger(AbstractLogger.INFO);//1
        error.nextLogger(file);
        file.nextLogger(console);
        return error;//3
    }
    public static void main(String[] args) {
        AbstractLogger chains = getChains();
        chains.logMessage(AbstractLogger.INFO,"This is an information.");//1
        chains.logMessage(AbstractLogger.DEBUG,"This is a debug level information.");//2
        chains.logMessage(AbstractLogger.ERROR,"This is an error information.");//3
    }
}

4.输出

相关推荐
蓝瑟1 天前
告别重复造轮子!业务组件多场景复用实战指南
前端·javascript·设计模式
Arva .1 天前
讲一下 Spring 中用到的设计模式
java·spring·设计模式
繁华似锦respect1 天前
lambda表达式中的循环引用问题详解
java·开发语言·c++·单例模式·设计模式·哈希算法·散列表
星月IWJ1 天前
领域驱动设计学习
java·学习·设计模式
_dindong1 天前
Linux网络编程:Reactor反应堆模式
linux·服务器·网络·设计模式·php
雨中飘荡的记忆1 天前
Step Builder模式实战
java·设计模式
Eren7Y琳1 天前
开箱即用构建应用环境:openEuler易获得性深度验证
redis·设计模式·架构
Unstoppable221 天前
八股训练营第 39 天 | Bean 的作用域?Bean 的生命周期?Spring 循环依赖是怎么解决的?Spring 中用到了那些设计模式?
java·spring·设计模式
闲人编程1 天前
微服务API网关设计模式
python·缓存·微服务·设计模式·系统安全·api·codecapsule
__万波__1 天前
二十三种设计模式(八)--装饰器模式
java·设计模式·装饰器模式