《责任链模式》

1.责任链模式介绍

  • 它为请求创建了一个接收者对象的链,这种模式对请求的发送者和接受者进行解耦,根据请求的类型,自行决定由哪个接收者去处理请求
  • 特点:责任链模式通常每个接受者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么他会把相同的请求传给下一个接收者,依次类推

2.代码实现之

  • 新建自定义请求类,模拟请求
class CustomRequest{
    private String name;
    private int level;

    public CustomRequest(String name, int level) {
        this.name = name;
        this.level = level;
    }
    public String getName() {
        return name;
    }
    public int getLevel() {
        return level;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setLevel(int level) {
        this.level = level;
    }
}
  • 新建链节点的同一接口
interface ResChain{

    void doChain(CustomRequest customRequest);
}

  • 新建链节点的统一抽象类
abstract class AbsChain implements ResChain{

   protected int level;
   protected ResChain next;

   public AbsChain() {}
   public void setNext(ResChain next) {
       this.next = next;
   }
}
  • 新建链接点A,B,C
class ChainA extends AbsChain{

    public ChainA(ResChain next) {
        this.next  = next;
        this.level = 1;
    }

    @Override
    public void doChain(CustomRequest request){
        Assert.notNull(request,"request must be not null");
        int type = request.getLevel();
        if (this.level >= type){
            System.out.println("ChainA handle request...."+request.getName());
        }else {
            if (this.next != null)
                this.next.doChain(request);
        }
    }
}

class ChainB extends AbsChain{

    public ChainB(ResChain next) {
        this.next  = next;
        this.level = 3;
    }

    @Override
    public void doChain(CustomRequest request) {
        Assert.notNull(request,"request must be not null");
        int type = request.getLevel();
        if (this.level >= type && type > 1){
            System.out.println("ChainB handle request...."+request.getName());
        }else {
            if (this.next !=null)
                this.next.doChain(request);
        }
    }
}
class ChainC extends AbsChain{

    public ChainC(ResChain next) {
        this.next  = next;
        this.level = 5;
    }

    @Override
    public void doChain(CustomRequest request) {
        Assert.notNull(request,"request must be not null");
        int type = request.getLevel();
        if (this.level >= type && type > 3){
            System.out.println("ChainC handle request...."+request.getName());
        }else {
            if (this.next != null)
                this.next.doChain(request);
        }
    }
}

  • 新建责任链测试类,构建单向责任链和环形责任链进行测试
class ResChainTest{

    public static void main(String[] args) {
        
        //构造单向处理链,链头去处理请求
        ChainA chain = new ChainA(new ChainB(new ChainC(null)));
        chain.doChain(new CustomRequest("Single_0",0));
        chain.doChain(new CustomRequest("Single_1",1));
        chain.doChain(new CustomRequest("Single_2",2));
        chain.doChain(new CustomRequest("Single_3",3));
        chain.doChain(new CustomRequest("Single_4",4));
        chain.doChain(new CustomRequest("Single_5",5));
        System.out.println("==================================================");

        //构造环形处理链,任意链节点都可以处理
        ChainA chainA = new ChainA(null);
        ChainB chainB = new ChainB(null);
        ChainC chainC = new ChainC(null);
        chainA.setNext(chainB);
        chainB.setNext(chainC);
        chainC.setNext(chainA);

        chainC.doChain(new CustomRequest("CircleC_0",0));
        chainC.doChain(new CustomRequest("CircleC_1",1));
        chainC.doChain(new CustomRequest("CircleC_2",2));
        chainC.doChain(new CustomRequest("CircleC_3",3));
        chainC.doChain(new CustomRequest("CircleC_4",4));
        chainC.doChain(new CustomRequest("CircleC_5",5));

        System.out.println("==================================================");
        
        chainB.doChain(new CustomRequest("CircleB_0",0));
        chainB.doChain(new CustomRequest("CircleB_1",1));
        chainB.doChain(new CustomRequest("CircleB_2",2));
        chainB.doChain(new CustomRequest("CircleB_3",3));
        chainB.doChain(new CustomRequest("CircleB_4",4));
        chainB.doChain(new CustomRequest("CircleB_5",5));
    }
}
  • 执行结果预览,可以发现环形链下,任意节点都可以处理请求,不影响最终结果
ChainA handle request....Single_0
ChainA handle request....Single_1
ChainB handle request....Single_2
ChainB handle request....Single_3
ChainC handle request....Single_4
ChainC handle request....Single_5
==================================================
ChainA handle request....CircleC_0
ChainA handle request....CircleC_1
ChainB handle request....CircleC_2
ChainB handle request....CircleC_3
ChainC handle request....CircleC_4
ChainC handle request....CircleC_5
==================================================
ChainA handle request....CircleB_0
ChainA handle request....CircleB_1
ChainB handle request....CircleB_2
ChainB handle request....CircleB_3
ChainC handle request....CircleB_4
ChainC handle request....CircleB_5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值