23种设计模式之责任链模式

news/2024/7/4 13:09:51

定义:将能够处理同一类请求的对象连成一条链,所提交的请求将沿着链传递,链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象。

场景:
1. 大学中,奖学金的审批
2. 公司中,公文的审批。

举例:员工请求,小于3天,主任审批;大于3天小于10天,经理审理;大于10天,小于30天,总经理审批;否则直接让他辞职回家

封装请假的基本信息

/**
 * 封装请假的基本信息
 */
public class LeaveRequest {
    private String empName;
    private int leaveDays;
    private String reason;

    public LeaveRequest(String empName, int leaveDays, String reason) {
        this.empName = empName;
        this.leaveDays = leaveDays;
        this.reason = reason;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getLeaveDays() {
        return leaveDays;
    }

    public void setLeaveDays(int leaveDays) {
        this.leaveDays = leaveDays;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }
}

将主任,经理,总经理等抽成一个抽象类

public abstract class Leader {
    protected String name;
    protected Leader nextLeader;//责任链上的后继对象(即上司)

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

    /**
     * 设置下一个对象
     */
    public void setNextLeader(Leader nextLeader) {
        this.nextLeader = nextLeader;
    }

    /**
     * 领导处理请求
     */
    public abstract void handleRequest(LeaveRequest request);
}

定义主任

public class DirectorLeader extends Leader{

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

    @Override
    public void handleRequest(LeaveRequest request) {
        if(request.getLeaveDays()<3){
            System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",理由:"+request.getReason());
            System.out.println("主任:"+this.name+",审批通过!");
        }else{
          if(nextLeader!=null){
              this.nextLeader.handleRequest(request);
          }
        }
    }
}

定义经理

public class ManagerLeader extends Leader {
    public ManagerLeader(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays() < 10) {
            System.out.println("员工:" + request.getEmpName() + "请假,天数:" + request.getLeaveDays() + ",理由:" + request.getReason());
            System.out.println("经理:" + this.name + ",审批通过!");
        } else {
            if (nextLeader != null) {
                nextLeader.handleRequest(request);
            }
        }
    }
}

定义总经理

public class GeneralManager extends Leader {
    public GeneralManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays() < 30) {
            System.out.println("员工:" + request.getEmpName() + "请假,天数:" + request.getLeaveDays() + ",理由:" + request.getReason());
            System.out.println("总经理:" + this.name + ",审批通过!");
        } else {
            System.out.println(request.getEmpName() + "居然请假" + request.getLeaveDays() + "天!" + request.getEmpName() + "明天不用上班了");
        }
    }
}

测试

 DirectorLeader director = new DirectorLeader("张主任");
        ManagerLeader manager = new ManagerLeader("王经理");
        GeneralManager generalManager = new GeneralManager("周总");
        director.setNextLeader(manager);
        manager.setNextLeader(generalManager);

        LeaveRequest request = new LeaveRequest("杨过", 30, "救姑姑小龙女");
        director.handleRequest(request);

责任链相当于代替了多个if …else if()语句


http://www.niftyadmin.cn/n/3648941.html

相关文章

Android Action Bar 详解篇

作为Android 3.0之后引入的新的对象&#xff0c;ActionBar可以说是一个方便快捷的导航神器。它可以作为活动的标题&#xff0c;突出活动的一些关键操作&#xff08;如“搜索”、“创建”、“共享”等&#xff09;、作为菜单的灵活使用&#xff0c;还可以实现类似TabWidget的标签…

[收藏]Microsoft 系统体系结构 v2.0

p.s.:要得到打包MSA2.0文档的请留言。MSA v2.0 有哪些新内容&#xff1f; 在平台产品更新以及客户和合作伙伴的反馈的基础上&#xff0c;Microsoft 系统体系结构有了进一步的发展。MSA v2.0 的新增内容包括&#xff1a;基于 Windows Server 2003 的新功能并对其充分加以利用 旨…

深度学习各类优化器大总结

一、优化算法设计原理 深度学习中的优化算法采用的原理是梯度下降法&#xff0c;即最小化目标函数 J ( θ ) J(\theta) J(θ)&#xff0c;最优化的求解过程&#xff0c;首先求解目标函数的梯度 ∇ J ( θ ) \nabla J(\theta) ∇J(θ)&#xff0c;然后将参数 θ \theta θ 向…

css 实现计数器_CSS计数器简介

css 实现计数器Counters are a big part of programming. They help keep tabs on loops by storing the number of times it’s been executed. Common variable names for increment counters are i, j and k. Before the days of modern CSS, keeping track of things on th…

23种模式之迭代器模式

场景 提供一种可以遍历聚合对象的方式。又称为&#xff1a;游标cursor模式。聚合对象&#xff1a;存储数据迭代器&#xff1a;遍历数据 迭代器接口 public interface MyIterator {void first(); //将游标指向第一个元素void next(); //将游标指向下一个元素boolean hasNext(…

javascript 代理_查看所有13个JavaScript代理陷阱

javascript 代理Proxies are a really cool JavaScript feature. If you like meta programming you probably are already familiar with them. In this article we are not going to get in to programming design patterns or get meta or even understand how proxies work…

Android 系统Action大全

常用Action说明&#xff1a; String ADD_SHORTCUT_ACTION 动作&#xff1a;在系统中添加一个快捷方式。. “android.intent.action.ADD_SHORTCUT”String ALL_APPS_ACTION 动作&#xff1a;列举所有可用的应用。 输入&#xff1a;无。 “android.intent.action.ALL_APPS”Strin…

23种设计模式之中介者模式

中介者模式本质 解耦多个部门对象之间的交互关系。每个对象都持有中介者对象的引用&#xff0c;只跟中介者对象打交道。我们通过中介者对象统一管理这些交互关系 android最常见的场景 MVC模式(其中的C)&#xff0c;控制器就是一个中介者对象。M和V都和它打交道 总经理接口:…