责任链模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ljz2016/article/details/76472224

责任链模式

1)责任链模式,很多对象由每一个对象对其下家的引用而连接起来形成一条链。
2)请求在这条链上传递,直到链上的某一个对象处理此请求为止。
3)发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织链和分配责任。
实现例子:

public interface Chain {
    void doFilter(Object o);
    void addHandler(Handler handler);
    void removeHandler(Handler handler);
}
public class HandlerChain  implements  Chain {
    private List<Handler> handlers=new ArrayList<>();
//    private Iterator<Handler> iterable;
    private static ThreadLocal<Iterator<Handler>> iteratorThreadLocal=new ThreadLocal<>();

    @Override
    public void doFilter(Object o) {
        Iterator<Handler> iterable=iteratorThreadLocal.get();
        if (iterable!= null && iterable.hasNext()) {
            Handler handler = iterable.next();
            if (handler != null) {
                handler.doSomething(this, o);
            }
        }
    }
    public void BeginFilter(Object o){
        iteratorThreadLocal.set( handlers.iterator());
        doFilter(o);
    }
    @Override
    public void addHandler(Handler handler) {
        handlers.add(handler);
    }

    @Override
    public void removeHandler(Handler handler) {
        handlers.remove(handler);
    }

}

public abstract class Handler {
    abstract void doSomething(Chain chain,Object o);
}

public class ManagerHandler extends Handler{

    @Override
    void doSomething(Chain chain, Object o) {
        MoneyCharge moneyCharge= (MoneyCharge) o;
        moneyCharge.setMoney(moneyCharge.getMoney()-1);
        System.out.println("manager抽取了"+moneyCharge.getName()+"的1万,还剩"+moneyCharge.getMoney()+"万.");
        if (moneyCharge.getMoney()<12){
            System.out.println(moneyCharge.getName()+"的钱少于12万,不能再被抽成了");
            return;
        }
        chain.doFilter(moneyCharge);
    }
}
public class BossHandler extends Handler {

    @Override
    void doSomething(Chain chain, Object o) {
        MoneyCharge moneyCharge= (MoneyCharge) o;
        moneyCharge.setMoney(moneyCharge.getMoney()-2);
        System.out.println("boss抽取了"+moneyCharge.getName()+"的2万,还剩"+moneyCharge.getMoney()+"万.");
        chain.doFilter(moneyCharge);
    }
}

public class MoneyCharge
{
    private String name;
    private double money;

    public MoneyCharge(String name, double money) {
        this.name=name;
        this.money=money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

public class TestT
{
    @Test
    public void test1(){
        HandlerChain chain=new HandlerChain();
        chain.addHandler(new ManagerHandler());
        chain.addHandler(new BossHandler());
        chain.addHandler(new BossHandler());
        chain.addHandler(new ManagerHandler());
        chain.BeginFilter(new MoneyCharge("小明",15));
        new Thread(()->
        chain.BeginFilter(new MoneyCharge("老王",16))).start();
    }

}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页