登录
首页 >  文章 >  java教程

责任链

来源:dev.to

时间:2024-09-16 12:18:38 494浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《责任链》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

责任链

顾名思义,责任链创建一系列接收者对象来处理请求。
该模式根据请求类型将请求的发送者和接收者解耦。
该模式属于行为模式。

在此模式中,请求的每个接收者对象都有对下一个对象的引用,如果它无法处理该请求,则该请求将传递到链中的下一个接收者。

让我们通过一个日志机制的例子来理解这一点,该机制根据消息(请求)的级别来记录消息

抽象记录器

package patterns.behavioral.chainofresponsibility;

public abstract class abstractlogger{
    /**
     * trace < debug < info < warn < error < fatal
     * which means if the level is info, then info, warn,error and fatal messages will be logged
     * but if the level is error then only error and fatal messages will be logged 
    */

    public static int debug = 1;
    public static int info = 2;
    public static int error = 3;
    protected int level;
    //next logger in the chain of responsibility
    private abstractlogger nextlogger;
    public void setnextlogger(abstractlogger logger){
        this.nextlogger = logger;
    }
    public void logmessage(int level, string message){
        //if the logging level of the message is greater than the current logger's level then it will be logged 
        //example if level = error and this.level = info then the message will be logged as info has a higher priority than error
        if(this.level <=level){
            write(message);
        }
        // else the message/request will be passed down to the next logger/object in the chain
        else{
            if(nextlogger!=null){
                nextlogger.logmessage(level, message);
            }
        }
    }
    abstract void write(string message);
}

concreteloggers

package patterns.behavioral.chainofresponsibility;

public class debuglogger extends abstractlogger {
    private string classname = this.getclass().getsimplename();
    private string logger   = "debug";
    public debuglogger(){
        this.level = 1;
    }

    @override
    void write(string message) {
        system.out.println(classname+":"+logger+":"+message);
    }

}

package patterns.behavioral.chainofresponsibility;

public class infologger extends abstractlogger {
    private string classname = this.getclass().getsimplename();
    private string logger   = "info";
    public infologger(){
        this.level = 2;
    }

    @override
    void write(string message) {
        system.out.println(classname+":"+logger+":"+message);
    }

}
package patterns.behavioral.chainofresponsibility;

public class errorlogger extends abstractlogger {
    private string classname = this.getclass().getsimplename();
    private string logger   = "error";
    public errorlogger(){
        this.level = 3;
    }

    @override
    void write(string message) {
        system.out.println(classname+":"+logger+":"+message);
    }

}

主要

package patterns.behavioral.chainofresponsibility;

public class main {
    public static abstractlogger intializeloggers(){
        abstractlogger errorlogger = new errorlogger(); //level = 3;
        abstractlogger infologger = new infologger(); //level = 2;
        abstractlogger debuglogger = new debuglogger(); // level = 1;
        errorlogger.setnextlogger(infologger);
        infologger.setnextlogger(debuglogger);
        return errorlogger;// return the highest priority logger first


    }
    public static void main(string args[]){
        // initialize the chain of responsible objects
        abstractlogger logger  = intializeloggers();

        //pass the request down the responsibility chain
        //logging level 3 logger
        logger.logmessage(3, "log this error message");
        //loggin level 2 logger
        logger.logmessage(2, "info level log message");
        //logging level 1 logger
        logger.logmessage(1, "debug level log message");
    }
}

输出:

ErrorLogger:ERROR:log this error message
InfoLogger:INFO:info level log message
DebugLogger:DEBUG:debug level log message

要点

  • 遵循lsp(里氏替换原则,即实体设计模式)。
  • 遵循坚实原则的srp。
  • 遵循 ocp 的坚实原则,因为我们可以添加更多记录器,如跟踪、致命等,而无需修改现有代码。
  • 也遵循 isp。

好了,本文到此结束,带大家了解了《责任链》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>