20-责任链模式

20-责任链模式

概念

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

责任链的好处:
当客户提交一个请求时,请求是沿链传递直至有一个ConcreteHandler对象处理它。
接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是责任链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持它所欲的候选接收者的引用。
可以随时的增加或修改处理一个请求的结构。增强了给对象指派职责的灵活性
需要注意,一个请求有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理

例子

领导处理请假和加薪

package com.gof.chainOfResponsibility;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Request {
    private String type;

    private int num;

    private String content;
}
package com.gof.chainOfResponsibility;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
public abstract class Manager {
    private String name;

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

    private Manager superior;

    public void setSuperior(Manager superior) {
        this.superior = superior;
    }

    public Manager getSuperior() {
        return superior;
    }

    public String getName() {
        return name;
    }

    public abstract void handleRequest(Request request);
}
package com.gof.chainOfResponsibility;

import java.util.Objects;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
public class CommonManager extends Manager {

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

    @Override
    public void handleRequest(Request request) {
        if (request.getType().equals("请假") && request.getNum() <= 2) {
            System.out.printf("%s: %s %d 天, 被批准.\n", getName(), request.getContent(), request.getNum());
        } else {
            if (Objects.nonNull(getSuperior())) {
                getSuperior().handleRequest(request);
            }
        }
    }
}
package com.gof.chainOfResponsibility;

import java.util.Objects;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
public class Majordomo extends Manager {
    public Majordomo(String name) {
        super(name);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getType().equals("请假") && request.getNum() < 5) {
            System.out.printf("%s: %s %d天, 被批准.\n", getName(), request.getContent(), request.getNum());
        } else {
            if (Objects.nonNull(getSuperior())) {
                getSuperior().handleRequest(request);
            }
        }
    }
}
package com.gof.chainOfResponsibility;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
public class GeneralManager extends Manager {
    public GeneralManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getType().equals("请假")) {
            System.out.printf("%s: %s %d 天, 被批准.\n", getName(), request.getContent(), request.getNum());
        } else if (request.getType().equals("加薪")) {
            if (request.getNum() <= 500) {
                System.out.printf("%s: %s %d 元, 被批准.\n", getName(), request.getContent(), request.getNum());
            } else {
                System.out.printf("%s: %s %d 元, 再看吧.\n", getName(), request.getContent(), request.getNum());
            }
        }
    }
}
package com.gof.chainOfResponsibility;

/**
 * 功能描述
 *
 * @since 2023-03-01
 */
public class Client {
    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 request1 = new Request("请假", 2, "小菜请假");
        Request request2 = new Request("请假", 4, "小菜请假");
        Request request3 = new Request("加薪", 500, "小菜加薪");
        Request request4 = new Request("加薪", 1000, "小菜加薪");

        commonManager.handleRequest(request1);
        commonManager.handleRequest(request2);
        commonManager.handleRequest(request3);
        commonManager.handleRequest(request4);

    }
}
posted @ 2023-03-01 10:16  Oh,mydream!  阅读(17)  评论(0编辑  收藏  举报