Lying design pattern notes (xxi) の duty chain.

Duty chain.

definition

A plurality of object a chance to handle the request, in order to avoid the coupling between the sender and recipient of the request. The object is connected into a chain, and pass the request along the chain until an object handles it so far.

UML diagrams

benefit

  • When the client submits a request, the request is passed along the chain until an object handles a ConcreteHandler it
  • Receiver and sender have no other party with clear information, and the chain object itself does not know the structure of the chain. When the results of interconnected objects simplify the chain of responsibility, they need only to keep a reference to its successor, without the need to keep all his candidates cited the recipient.
  • You may increase or modify the structure of a request at any time, and enhance the flexibility of assigning responsibilities to objects

But note that: a request is likely to end of the chain are not treated, or because treatment is not properly configured can not be obtained.

For chestnuts

Employees apply for a raise, you need to take the process

Talk is cheap, show me the code

(Shit less, grading up)

/**
 * 管理者
 * Created by callmeDevil on 2019/12/14.
 */
public abstract class Manager {

    protected String name;
    // 上级
    protected Manager superior;

    public Manager(String name){
        this.name = name;
    }

    // 设置上级
    public void setSuperior(Manager superior){
        this.superior = superior;
    }

    // 申请请求
    public abstract void requestApplications(Request request);

}
/**
 * 经理
 * Created by callmeDevil on 2019/12/14.
 */
public class CommonManager extends Manager {

    public CommonManager(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        // 经理的权限可以批准2天内的假期
        if ("请假".equals(request.getRequestType()) && request.getNumber() <= 2) {
            System.out.println(String.format("%s : %s 数量%s 被批准", name, request.getRequestContent(), request.getNumber()));
        } else {
            // 其余申请转交上级
            if (superior != null) {
                superior.requestApplications(request);
            }
        }
    }

}
/**
 * 总监
 * Created by callmeDevil on 2019/12/14.
 */
public class Majordomo extends Manager {

    public Majordomo(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        // 总监的权限可以批准5天内的假期
        if ("请假".equals(request.getRequestType()) && request.getNumber() <= 5) {
            System.out.println(String.format("%s : %s 数量%s 被批准", name, request.getRequestContent(), request.getNumber()));
        } else {
            // 其余申请转交上级
            if (superior != null) {
                superior.requestApplications(request);
            }
        }
    }

}
/**
 * 总经理
 * Created by callmeDevil on 2019/12/14.
 */
public class GeneralManager extends Manager {

    public GeneralManager(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        // 总经理可以批准任意天的假期
        if ("请假".equals(request.getRequestType())) {
            System.out.println(String.format("%s : %s 数量%s 被批准", name, request.getRequestContent(), request.getNumber()));
        }
        // 加薪在500内,没问题
        else if ("加薪".equals(request.getRequestType()) && request.getNumber() <= 500) {
            System.out.println(String.format("%s : %s 数量%s 被批准", name, request.getRequestContent(), request.getNumber()));
        }
        // 加薪超过500要考虑一下
        else if ("加薪".equals(request.getRequestType()) && request.getNumber() > 500) {
            System.out.println(String.format("%s : %s 数量%s 再说吧", name, request.getRequestContent(), request.getNumber()));
        }
    }

}
/**
 * 申请类
 * Created by callmeDevil on 2019/12/14.
 */
public class Request {

    // 申请级别
    private String requestType;
    // 申请内容
    private String requestContent;
    // 数量
    private int number;

    // 省略get set
}
public class Test {
    public static void main(String[] args) {
        CommonManager commonManager = new CommonManager("中将");
        Majordomo majordomo = new Majordomo("大将");
        GeneralManager generalManager = new GeneralManager("元帅");
        // 设置上级
        commonManager.setSuperior(majordomo);
        majordomo.setSuperior(generalManager);

        Request request = new Request();
        request.setRequestType("请假");
        request.setRequestContent("不给请假我不干了");
        request.setNumber(1);
        commonManager.requestApplications(request); // 客户端的申请都是由经理发起的,但实际上决策是谁,客户端不知道

        Request request2 = new Request();
        request2.setRequestType("请假");
        request2.setRequestContent("不给请假我不干了");
        request2.setNumber(4);
        commonManager.requestApplications(request2);

        Request request3 = new Request();
        request3.setRequestType("加薪");
        request3.setRequestContent("不给加薪我不干了");
        request3.setNumber(500);
        commonManager.requestApplications(request3);

        Request request4 = new Request();
        request4.setRequestType("加薪");
        request4.setRequestContent("不给加薪我不干了");
        request4.setNumber(1000);
        commonManager.requestApplications(request4);
    }
}

operation result

中将 : 不给请假我不干了 数量1 被批准
大将 : 不给请假我不干了 数量4 被批准
元帅 : 不给加薪我不干了 数量500 被批准
元帅 : 不给加薪我不干了 数量1000 再说吧

Guess you like

Origin www.cnblogs.com/call-me-devil/p/12040381.html