Java chain of responsibility pattern

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/walkerwqp/article/details/91360175

As the name suggests, the chain of responsibility pattern (Chain of Responsibility Pattern) created a chain recipient object to the request. This type of model given request, to the request sender and receiver decoupling. This type of design pattern belongs behavioral patterns.

In this mode, each receiver typically contains a reference to another recipient. If an object can not process the request, it will pass the same request to the next recipient, and so on.

It is intended to: avoid requesting the sender and receiver are coupled together, so that a plurality of objects are likely to receive a request to connect these objects form a chain, and the request is transmitted along the chain until an object handles it so far.

: Mainly to solve the handler handler on duty chain responsible for handling requests, customers only need to send a request to transfer details of the treatment and the chain of responsibility can, without concern for the request of the request, so the chain of responsibility will request the sender and request decoupled.

When to use: when dealing with many channel messages to filter.

How to solve: interception classes implement a unified interface.

Key Code: Handler polymerization inside its own judgment is appropriate in HandlerRequest, if conditions did not reach down the pass, set to go before whom passed.

Application examples: 1, The Dream of Red Mansions "Drumming pass to spend." 2, JS events bubbling. 3, JAVA WEB Encoding processing in the Apache Tomcat, Struts2 interceptor, jsp servlet's Filter.

Advantages: 1, decouple. It requests the sender and receiver decoupling. 2, the object is simplified. Such that the object does not need to know the structure of the chain. 3, enhance the flexibility of assigning responsibilities to objects. By changing the members of the chain or the mobilization of their order, allows to dynamically add or delete liability. 4, adding new class request processing easy.

Disadvantages: 1, does not necessarily guarantee the request is received. 2, system performance will be affected, but not very convenient when performing debugging code may cause the cycle call. 3, may not be readily observable characteristic runtime hinder debugging.

Be used: 1, a plurality of the same object can process a request, the request is processed concrete which object is determined automatically by the run time. 2, in a case where the recipient is not explicitly specified, submit a request to a plurality of objects. 3, a set of objects may be dynamically assigned to process the request.

Note: encountered in many applications in JAVA WEB.

AbstractLogger.java

public abstract class AbstractLogger { //创建抽象的记录器类
    
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;
    
    protected int level;
    
    //责任链中的下一个元素
    protected AbstractLogger nexLogger;
    
    public void setNextLogger(AbstractLogger nexLogger) {
        this.nexLogger = nexLogger;
    }
    
    public void logMessage(int level, String message) {
        if (this.level <= level) {
            write(message);
        }
        if (nexLogger != null) {
            nexLogger.logMessage(level, message);
        }
    }
    
    abstract protected void write(String message);
    
}

ConsoleLogger.java

public class ConsoleLogger extends AbstractLogger {// create an extension of the entity class record class of
    
    public ConsoleLogger (Level int) {
        this.level = Level;
    }
    
    @Override
    protected void Write (String Message) {
        System.out.println ( "Standard :: Logger Console: "+ Message);
    }
    
}

ErrorLogger.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);
    }
}

FileLogger.java

public class FileLogger extends AbstractLogger {
    public FileLogger(int level) {
        this.level = level;
    }
    
    @Override
    protected void write(String message) {
        System.out.println("File::Logger: " + message);
    }
}

ChainPatternDemo.java

public class ChainPatternDemo {// create different types of recorders, giving them a different error levels, and set the next record in each recorder. The next representative of each recorder recorder is part of the chain
    
    Private static AbstractLogger getChainOfLogger () {
        
        AbstractLogger errorLogger = new new ErrorLogger (AbstractLogger.ERROR);
        AbstractLogger FileLogger the FileLogger new new = (AbstractLogger.DEBUG);
        AbstractLogger ConsoleLogger = new new ConsoleLogger (AbstractLogger.INFO);
        
        errorLogger.setNextLogger (FileLogger);
        fileLogger.setNextLogger (ConsoleLogger);
        
        return errorLogger;
        
    }
    
    public static void main (String [] args) {
        AbstractLogger loggerChain getChainOfLogger = ();
        loggerChain.logMessage (AbstractLogger.INFO, "This is an information");
        loggerChain.logMessage(AbstractLogger.DEBUG, "This is a debug level information");
        loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information");
    }
    
}

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91360175