职责链模式
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,
将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。

角色
抽象处理者角色(Handler):定义出一个处理请求的接口。如果需要,接口可以定义 出一个方法以设定和返回对下家的引用。这个角色通常由一个Java抽象类或者Java接口实现。
具体处理者角色(ConcreteHandler):具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。
抽象处理者角色

例子:
1。sales

package com.test.chain_of_responsibilitymodel;

public class Sales extends PriceCaseBase{

    @Override
    public void postPriceUp(float price) {
        // TODO Auto-generated method stub
        if(price>=90){
            System.out.println("90以内"+this.getClass().getName()+"批准了!卖"+price);
        }else {
            priceCaseBase.postPriceUp(price);
        }
    }

}

2。SalesManger

package com.test.chain_of_responsibilitymodel;

public class SalesManger extends PriceCaseBase{

    @Override
    public void postPriceUp(float price) {
        // TODO Auto-generated method stub
        if(price>=70){
            System.out.println("70以内"+this.getClass().getName()+"批准了!卖"+price);
        }else {
            priceCaseBase.postPriceUp(price);
        }
    }

}

3。PartManger

package com.test.chain_of_responsibilitymodel;

public class PartManger extends PriceCaseBase{

    @Override
    public void postPriceUp(float price) {
        // TODO Auto-generated method stub
        if(price>=50){
            System.out.println("50以内"+this.getClass().getName()+"批准了!卖"+price);
        }else {
            priceCaseBase.postPriceUp(price);
        }
    }

}

4。CompanyManger

package com.test.chain_of_responsibilitymodel;

public class CompanyManger extends PriceCaseBase{

    @Override
    public void postPriceUp(float price) {
        // TODO Auto-generated method stub
        if(price>=30){
            System.out.println("30以内"+this.getClass().getName()+"批准了!卖"+price);
        }else {
            System.out.println(price+"太亏了,卖不了!");
        }
    }

}

5。interface PriceCaseBase

package com.test.chain_of_responsibilitymodel;

public abstract class PriceCaseBase {

    // 子类可以访问到自身的引用
    protected PriceCaseBase priceCaseBase;

    public void setPriceCaseBase(PriceCaseBase priceCaseBase) {
        this.priceCaseBase = priceCaseBase;
    }

    public abstract void postPriceUp(float price);

}

6。PriceCaseBaseFactory

package com.test.chain_of_responsibilitymodel;

public class PriceCaseBaseFactory {

    /**
     * 
     * @return
     */
    public static PriceCaseBase createPriceCaseBaseFactory() {
        PriceCaseBase sales = new Sales();
        PriceCaseBase salesManger = new SalesManger();
        PriceCaseBase partManger = new PartManger();
        PriceCaseBase companyManger = new CompanyManger();

        // 依次设置直接后继
        sales.setPriceCaseBase(salesManger);
        salesManger.setPriceCaseBase(partManger);
        partManger.setPriceCaseBase(companyManger);

        return sales;
    }

}

7。Test

package com.test.chain_of_responsibilitymodel;



public class Test {

    PriceCaseBase priceCaseBase;

    public void setPriceCaseBase(PriceCaseBase priceCaseBase) {
        this.priceCaseBase = priceCaseBase;
    }

    public void doCasePrice(float price) {
        priceCaseBase.postPriceUp(price);
    }

    public static void main(String[] agrs) {
        Test test = new Test();
        test.setPriceCaseBase(PriceCaseBaseFactory.createPriceCaseBaseFactory());

        for (int i = 0; i < 50; i++) {
            System.out.print("=======" + i+":");
            float price =(float) (Math.random()*100);
            test.doCasePrice(price);
        }
    }
}

8.结果

=======0:12.921482太亏了,卖不了!
=======1:15.293591太亏了,卖不了!
=======2:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖72.087074
=======3:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖48.80181
=======4:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖76.63781
=======5:15.493307太亏了,卖不了!
=======6:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖87.30026
=======7:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖76.97786
=======8:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖32.80416
=======9:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖40.209476
=======10:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖35.908688
=======11:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖42.464153
=======12:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖83.58707
=======13:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖64.86954
=======14:9.98403太亏了,卖不了!
=======15:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖82.65527
=======16:11.788233太亏了,卖不了!
=======17:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖48.04178
=======18:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖67.012344
=======19:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖51.525536
=======20:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖52.2112
=======21:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖67.39455
=======22:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖55.068405
=======23:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖53.07613
=======24:22.03274太亏了,卖不了!
=======25:3.1497579太亏了,卖不了!
=======26:10.088352太亏了,卖不了!
=======27:16.632261太亏了,卖不了!
=======28:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖81.409966
=======29:90以内com.test.chain_of_responsibilitymodel.Sales批准了!卖99.8564
=======30:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖76.012665
=======31:21.622719太亏了,卖不了!
=======32:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖64.79263
=======33:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖57.329395
=======34:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖50.622337
=======35:90以内com.test.chain_of_responsibilitymodel.Sales批准了!卖94.80636
=======36:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖83.27312
=======37:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖37.24371
=======38:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖52.56701
=======39:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖83.86704
=======40:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖48.795223
=======41:23.143074太亏了,卖不了!
=======42:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖38.384327
=======43:50以内com.test.chain_of_responsibilitymodel.PartManger批准了!卖57.880505
=======44:12.155732太亏了,卖不了!
=======45:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖38.517277
=======46:70以内com.test.chain_of_responsibilitymodel.SalesManger批准了!卖72.23591
=======47:90以内com.test.chain_of_responsibilitymodel.Sales批准了!卖97.363785
=======48:30以内com.test.chain_of_responsibilitymodel.CompanyManger批准了!卖38.471447
=======49:10.121493太亏了,卖不了!