设计模式之责任链的学习思考

责任链模式,顾名思义,每个对象之间形成一条链,这条链形成的基础是各级对象的责任;当一个消息请求发来时,依次传递,哪个对象符合消息处理的条件就处理。
这个模式可以让消息的发送者不清楚消息的处理者具体是谁,职责链将请求的发送者和请求的处理者解耦了。属于设计模式里的行为型模式

设计:

  • 一个抽象类,属性有各级指数,后继对象,方法有抽象和非抽象:非抽象是判断谁处理,抽象是各级对象具体处理方法;
  • 各实现抽象类的具体类
  • public实现类

小例子:

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

    int level;      //责任链上的各级指数

    AbstractLogger nextLogger;//持有后继的责任对象

    public void setNextLogger(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);//各级责任对象的独有处理方法
}

class ConsoleLogger extends AbstractLogger{
    public ConsoleLogger(int level){
        this.level=level;
    }


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

    }

}


class ErrorLogger extends AbstractLogger{
    public ErrorLogger(int level){
        this.level=level;
    }


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

    }

}

class FileLogger extends AbstractLogger{
    public FileLogger(int level){
        this.level=level;
    }


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

    }

}
public class ChainPatternDemo {
    private static AbstractLogger getChainOfLoggers(){
        AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
        AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
        AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);

        errorLogger.setNextLogger(fileLogger);
        fileLogger.setNextLogger(consoleLogger);

        return errorLogger;
    }

    public static void main(String[] args) {
        AbstractLogger loggerChain=getChainOfLoggers();
        loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");
        loggerChain.logMessage(AbstractLogger.DEBUG, "This is an debug level information.");
        loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information.");
    }
}

输出:

    ConsoleLogger:This is an information.
    FileLogger:This is an debug level information.
    ConsoleLogger:This is an debug level information.
    ErrorLogger:This is an error information.
    FileLogger:This is an error information.
    ConsoleLogger:This is an error information.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值