责任链模式的简单实现

简单的责任链模式实现

话不多说,直接上代码

接口类

package com.zj.demo.demo.chain;


public interface Chain {
	// 接口
    void doFilter(Chain chain);

}

接口实现1

package com.zj.demo.demo.chain;

public class ChainOneImpl implements Chain{
    @Override
    public void doFilter(Chain chain) {
        System.out.println("chain one impl");
        chain.doFilter(chain);
        System.out.println("chain one impl end ");
    }
}

接口实现2

package com.zj.demo.demo.chain;

public class ChainTwoImpl implements Chain{
    @Override
    public void doFilter(Chain chain) {

        System.out.println("chain two impl");
        chain.doFilter(chain);
        System.out.println("chain two impl end");
    }
}

接口实现3

package com.zj.demo.demo.chain;

import java.util.ArrayList;
import java.util.List;

public class ChainListImpl implements Chain{


    private List<Chain> chainList = new ArrayList<>();
    private int num = 0;

    @Override
    public void doFilter(Chain chain) {
    	//遍历完结束返回
        if(num == chainList.size())return;
        // 遍历chainList里的Chain。
        chainList.get(num++).doFilter(this);
    }

    public void addChain(Chain chain){
        chainList.add(chain);
    }
}

main类

package com.zj.demo.demo.chain;

public class MainTest {


    public static void main(String[] args) {
		
        ChainListImpl chainList = new ChainListImpl();

        chainList.addChain(new ChainOneImpl());
        chainList.addChain(new ChainTwoImpl());
		
		// 把chainList传进去,为的就是重复进入ChainListImpl的doFilter方法。遍历所有的chain
        chainList.doFilter(chainList);

    }
}


输出日志

chain one impl
chain two impl
chain two impl end
chain one impl end 



换个方法及名称能更好的理清实质
*精简版*

接口

public interface Chain {
	//传进去ChainList。用于在doFilter里面调用下一个Chain.doFilter
    void doFilter(ChainList chainList);

}

实现1

package com.zj.demo.demo.chain;

public class ChainOneImpl implements Chain{
    public void doFilter(ChainList chainList) {
        System.out.println("chain one impl");
        chainList.nextChain();
        System.out.println("chain one impl end ");
    }

}

实现2

package com.zj.demo.demo.chain;

public class ChainTwoImpl implements Chain{
    @Override
    public void doFilter(ChainList chainList) {

        System.out.println("chain two impl");
        chainList.nextChain();
        System.out.println("chain two impl end");
    }

}

关键的链路逻辑

package com.zj.demo.demo.chain;

import java.util.ArrayList;
import java.util.List;

public class ChainList {


    private List<Chain> chainList = new ArrayList<>();
    private int num = 0;
	
	/**
	用nextChain,执行下一个链路
	*/
    public void nextChain() {
        if(num == this.chainList.size())return;
        this.chainList.get(num++).doFilter(this);

    }

    public void addChain(Chain chain){
        chainList.add(chain);
    }
}

主类

package com.zj.demo.demo.chain;

public class MainTest {


    public static void main(String[] args) {

        ChainList chainList = new ChainList();

        chainList.addChain(new ChainOneImpl());
        chainList.addChain(new ChainTwoImpl());

        chainList.nextChain();

    }
}

总结一下本质就是。iterate -> doFilter1 -> iterate -> doFilter2

遍历的时候进行遍历

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值