每天一个设计模式之责任链模式(Chain of Responsibility Pattern)

为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

一、UML类图

每天一个设计模式之责任链模式(Chain of Responsibility Pattern)_第1张图片

二、代码示例

每天一个设计模式之责任链模式(Chain of Responsibility Pattern)_第2张图片

处理器抽象类

public abstract class Logger {
    public static int INFO = 1, DEBUG = 2, ERROR = 3;

    public Logger(int level) {
        this.level = level;
    }

    protected int level;

    protected Logger next;
    protected static Logger link(Logger first, Logger ... loggers) {
        Logger head = first;
        for(Logger logger : loggers) {
            head.next = logger;
            head = logger;
        }
        return first;
    }

    public void logMessage(int level, String message) {
        if (this.level <= level)
            write(message);
        if (next != null)
            next.logMessage(level, message);
    }

    abstract protected void write(String message);
}

处理器类

public class ConsoleLogger extends Logger {
    public ConsoleLogger(int level) {
        super(level);
    }

    protected void write(String message) {
        System.out.println("Console::logger: " + message);
    }
}

public class DebugLogger extends Logger {
    public DebugLogger(int level) {
        super(level);
    }

    protected void write(String message) {
        System.out.println("Debug::logger: " + message);
    }
}

public class ErrorLogger extends Logger {
    public ErrorLogger(int level) {
        super(level);
    }

    protected void write(String message) {
        System.out.println("Error::logger: " + message);
    }
}

客户类

public class Client {
    public static void main(String[] args) {
        Logger conLogger = new ConsoleLogger(Logger.INFO);
        Logger debLogger = new DebugLogger(Logger.DEBUG);
        Logger errLogger = new ErrorLogger(Logger.ERROR);

        Logger linkedLogger = Logger.link(conLogger, debLogger, errLogger);

        linkedLogger.logMessage(Logger.INFO, "this is normal information");
        System.out.println("-----------");
        linkedLogger.logMessage(Logger.DEBUG, "this is debug information");
        System.out.println("-----------");
        linkedLogger.logMessage(Logger.ERROR, "this is error information");
    }
}

运行结果

Console::logger: this is normal information
-----------
Console::logger: this is debug information
Debug::logger: this is debug information
-----------
Console::logger: this is error information
Debug::logger: this is error information
Error::logger: this is error information

Process finished with exit code 0

三、参考

  1. https://refactoring.guru/design-patterns/chain-of-responsibility/java/example
  2. https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html
  3. https://www.geeksforgeeks.org/chain-responsibility-design-pattern/
  4. https://www.javatpoint.com/chain-of-responsibility-pattern

设计模式系列博文导航

一、创建型 - 5种

原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)

助记语:原抽建工单

二、结构型 - 8种

享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)

过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)

助记语:想呆室外,过桥组装

三、行为型 - 11种

责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)

观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)

备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)

助记语:责令解中谍,观测状被模仿

你可能感兴趣的:(软件设计,设计模式,责任链模式,java)