首页 > Java > java教程 > 正文

Chain of responsibility

DDD
发布: 2024-09-18 15:47:02
原创
901 人浏览过

Chain of responsibility

As the name suggests, a chain of responsibility creates a chain of receiver objects to handle requests.
This pattern decouples the request's sender and receiver based on the request type.
This pattern comes under the Behavioural pattern.

In this pattern each receiver object of the request has a reference to the next object if it can not handle the request, the request is passed down to the next receiver in the chain.

Let's understand this by taking the example of a logging mechanism that logs messages based on the level of the message (request)

AbstractLogger

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 
    */
    //higher the number higher the priority
    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 lower 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);
    }

}
登录后复制

Main

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");
    }
}
登录后复制

Output:

ErrorLogger:ERROR:log this error message
InfoLogger:INFO:info level log message
DebugLogger:DEBUG:debug level log message
登录后复制

Key points

  • Follows LSP (The Liskov substitution principle i.e. solid design pattern).
  • Follows SRP of solid principle.
  • Follows OCP of solid principle as we can add more Loggers like trace, fatal, etc without modifying existing code at all.
  • Follows ISP as well.

以上是Chain of responsibility的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!