设计模式-责任链模式

模式简介

       责任链模式是一种对象的行为模式.在责任链模式里面,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上进行传递,直到这个链上的某一个对象来处理请求,发送这个请求的客户端并不知道哪一个对象最终来处理这个请求.这使得系统可以在不影响客户端的情况下来动态的重新组织和分配责任.

案例(申请加薪和请假)

模拟日常公司请假和申请调薪流程,有经理、cto、boss三个角色的管理人员

Request.java
package com.codetears.ChainOfResponsibility;

import lombok.Data;

import java.math.BigDecimal;

@Data
public class Request {

    /** 请求类型(1:请假 2:加薪)*/
    private int type;

    /** 请假天数/加薪金额*/
    private BigDecimal amount;

}
Manager.java
package com.codetears.ChainOfResponsibility;

import lombok.Data;

/**
 * 管理者
 */
@Data
abstract class Manager {

    protected String name;
    protected Manager supManager;

    abstract public void deal(Request request);

}
CommonManager.java
package com.codetears.ChainOfResponsibility;

import java.math.BigDecimal;

/**
 * 经理
 */
public class CommonManager extends Manager {

    public CommonManager(Manager supManager, String name) {
        super.name = name;
        super.supManager = supManager;
    }

    @Override
    public void deal(Request request) {
        int type = request.getType();
        BigDecimal amount = request.getAmount();
        switch (type) {
            case 1:
                if (amount.compareTo(new BigDecimal("3")) > 0) {
                    supManager.deal(request);
                } else {
                    System.out.printf(this.name + "批准");
                }
                break;
            case 2:
                if (amount.compareTo(new BigDecimal("5000")) > 0) {
                    supManager.deal(request);
                } else {
                    System.out.printf(this.name + "批准");
                }
        }
    }
}
CtoManager.java
package com.codetears.ChainOfResponsibility;

import java.math.BigDecimal;

/**
 * 经理
 */
public class CtoManager extends Manager {

    public CtoManager(Manager supManager, String name) {
        super.name = name;
        super.supManager = supManager;
    }

    @Override
    public void deal(Request request) {
        int type = request.getType();
        BigDecimal amount = request.getAmount();
        switch (type) {
            case 1:
                if (amount.compareTo(new BigDecimal("10")) > 0) {
                    supManager.deal(request);
                } else {
                    System.out.printf(this.name + "批准");
                }
                break;
            case 2:
                if (amount.compareTo(new BigDecimal("8000")) > 0) {
                    supManager.deal(request);
                } else {
                    System.out.printf(this.name + "批准");
                }
        }
    }
}
BossManager.java
package com.codetears.ChainOfResponsibility;

import java.math.BigDecimal;

/**
 * 经理
 */
public class BossManager extends Manager {

    public BossManager(String name) {
        super.name = name;
    }

    @Override
    public void deal(Request request) {
        int type = request.getType();
        BigDecimal amount = request.getAmount();
        switch (type) {
            case 1:
                if (amount.compareTo(new BigDecimal("20")) > 0) {
                    System.out.printf(this.name + "拒绝申请");
                } else {
                    System.out.printf(this.name + "批准");
                }
                break;
            case 2:
                if (amount.compareTo(new BigDecimal("10000")) > 0) {
                    System.out.printf(this.name + "拒绝申请");
                } else {
                    System.out.printf(this.name + "批准");
                }
        }
    }
}
Client.java
package com.codetears.ChainOfResponsibility;

import java.math.BigDecimal;

public class Client {

    public static void main(String[] args) {
        Manager bossManager = new BossManager("王老板");
        Manager ctoManager = new CtoManager(bossManager, "赵总监");
        Manager commonManager = new CommonManager(ctoManager,"张经理");


//        Request request = new Request();
//        request.setType(1);
//        request.setAmount(new BigDecimal("115"));
//
//        commonManager.deal(request);

        Request request1 = new Request();
        request1.setType(2);
        request1.setAmount(new BigDecimal("9500"));

        commonManager.deal(request1);

    }
}

 

你可能感兴趣的:(责任链模式,设计模式,java)