设计模式: 责任链模式

目录

一,责任链模式

二,特点

四,实现步骤

五,代码


一,责任链模式

责任链模式(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.输出

相关推荐
IT小白架构师之路15 小时前
常用设计模式系列(十八)-责任链模式
设计模式·责任链模式
源代码•宸1 天前
深入浅出设计模式——行为型模式之观察者模式 Observer
开发语言·c++·经验分享·观察者模式·设计模式·raii
快起来别睡了1 天前
前端设计模式:让代码更优雅的“万能钥匙”
前端·设计模式
使二颗心免于哀伤2 天前
《设计模式之禅》笔记摘录 - 14.组合模式
笔记·设计模式·组合模式
原则猫2 天前
装饰器工程运用-埋点
设计模式
愿天堂没有C++2 天前
剑指offer第2版——面试题2:实现单例
c++·设计模式·面试
静谧之心2 天前
分层架构下的跨层通信:接口抽象如何解决反向调用
java·开发语言·设计模式·架构·golang·k8s·解耦
用户84913717547163 天前
JustAuth实战系列(第5期):建造者模式进阶 - AuthRequestBuilder设计解析
java·设计模式·架构
只因在人海中多看了你一眼3 天前
B.10.01.5-电商系统的设计模式应用实战
设计模式
希望_睿智3 天前
实战设计模式之代理模式
c++·设计模式·架构