设计模式(JAVA)——ChainOfResponsibility模式

1. ChainOfResponsibility模式

将多个对象组成一条职责链,然后按照它们在职责链上的顺序一个一个地找出到底由谁来负责,或者让请求一个一个的经过职责链中的对象进行处理。

1.1 ChainOfResponsibility模式的类图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LjIhphyf-1574600063119)(./resources/image/14/14-1_ChainOfResponsibility.png)]

登场角色:

  • Handler(处理者)
    定义了处理请求的接口

  • ConcreteHandler(具体的处理者)
    处理请求的具体实现者

  • Client(请求者)
    发送请求者

1.2 示例程序

类一览表

名字说明
Trouble要解决的问题,也是一个请求消息
Support解决问题的抽象类
NoSupport解决问题的一个具体类
LimitSupport解决问题的的一个具体类
OddSupport解决问题的一个具体类
SpecialSupprot解决问题的一个具体类
Main测试类

uml类图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JhKu4Gq6-1574600063121)(./resources/image/14/14-2.png)]

Support类

package xin.ajay.chain;

public abstract class Support {
    private String name;
    private Support next;

    public Support(String name){
        this.name = name;
    }

    public Support setNext(Support next){
        this.next = next;

        return next;
    }

    public final void support(Trouble trouble){
        //推卸责任式,tomcat使用了责任链式并非推卸责任式,这两者有不同,但都可称为责任链式。
        if(resolve(trouble)){
            done(trouble);
        } else if(next!=null){
            next.support(trouble);
        }else {
            fail(trouble);
        }


    }

    protected  void fail(Trouble trouble){
        System.out.println(trouble +"cannot be resolved");
    }

    protected  void done(Trouble trouble){
        System.out.println(trouble + " is resolved by " + this + ".");
    }

    protected abstract boolean resolve(Trouble trouble);

    @Override
    public String toString() {
        return "Support{" + "name='" + name + '\'' + '}';
    }
}

Trouble类

package xin.ajay.chain;

public class Trouble {
    private int number;

    public Trouble(int number){
        this.number = number;
    }

    public int getNumber(){
        return number;
    }

    public String toString(){
        return "[Trouble " + number + "]";
    }


}

NoSupport类

package xin.ajay.chain;

public class NoSupport extends Support {


    public NoSupport(String name) {
        super(name);
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        return false;
    }
}

LimitSupport类

package xin.ajay.chain;

public class LimitSupport extends Support {
    private int limit;

    public LimitSupport(String name, int limit) {
        super(name);
        this.limit = limit;
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        return trouble.getNumber() < limit;


    }
}

OddSupport类

package xin.ajay.chain;

public class OddSupport extends Support {

    public OddSupport(String name) {
        super(name);
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        return (trouble.getNumber() & 1) == 1;
    }
}

SpecialSupprot类

package xin.ajay.chain;

public class SpecialSupport extends Support {
    private int number;

    public SpecialSupport(String name, int number) {
        super(name);
        this.number = number;
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        return trouble.getNumber() == number;
    }
}

Main类

package xin.ajay.chain;

public class Main {
    public static void main(String[] args) {
        NoSupport noSupport = new NoSupport("noSupport");
        LimitSupport limitSupport = new LimitSupport("LimitSupport", 10);
        SpecialSupport specialSupport = new SpecialSupport("SpecialSupport", 40);
        LimitSupport limitSupport2 = new LimitSupport("LimitSupport2", 20);
        OddSupport oddSupport = new OddSupport("oddSupport");
        LimitSupport limistSupport3 = new LimitSupport("limistSupport3", 30);

        noSupport.setNext(limitSupport).setNext(specialSupport).setNext(limitSupport2).setNext(oddSupport).setNext(limistSupport3);

        for (int i = 0; i < 50; i++) {
            noSupport.support(new Trouble(i));
        }
    }
}
鸣谢:

GoF《设计模式》和结城浩的《图解设计模式》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值