zoukankan      html  css  js  c++  java
  • 设计模式:责任链模式

      将能够处理同一类请求的对象形成一条链,所提交的请求沿着链传递,链上的对象逐个判断是否有能力处理该请求

    如果能则处理,否则传递给下一个对象。

      例如:公司行政审批流程,斗地主游戏,田径项目中接力运动等。都是责任链模式 的运用。

      责任链可能是一条直线,一个环链或者是一个树结构的一部分。

    责任链涉及角色:

      抽象处理者角色(Handler):定义一个处理请求的接口。可以定义一个方法返回对下家对象的引用。

      具体处理者角色(ConcreteHandler):具体处理者接到请求后,可以处理该请求或者将请求传给下家。

    例子:

      模拟一个请假处理流程

      

    /**
    * 领导
    * 抽象类
    */
    public abstract class Leader {
    //子类实现
    protected String name;
    //下一个责任链对象
    protected Leader next;

    //处理请求抽象方法
    abstract void HandleLeaveNotes(LeaveNote note);

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

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

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Leader getNext() {
    return next;
    }

    public void setNext(Leader next) {
    this.next = next;
    }
    }

      

    **
     * 请假条
     */
    public class LeaveNote {
    
        private String departMent;
    
        private String name;
    
        private Integer days;
    
        private String reason;
    
        //...get set
    }
    

      

    /**
    * 部门领导
    */
    public class DepartManager extends Leader {


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

    @Override
    void HandleLeaveNotes(LeaveNote note) {
    //假如小于3天部门经理有权限
    if(note.getDays()<3){
    System.out.println("批准假条: "+ note.getName());
    }else{
    //否则移交到上级领导
    this.next.HandleLeaveNotes(note);
    }
    }
    }

      

    /**
    * 经理
    */
    public class Manager extends Leader {
    public Manager(String name) {
    super(name);
    }

    @Override
    void HandleLeaveNotes(LeaveNote note) {
    //假如大于3天小于7天经理有权限
    if (note.getDays() >= 3 && note.getDays() <= 7) {
    System.out.println("批准假条: " + note.getName());
    } else {
    //否则移交到老板
    this.next.HandleLeaveNotes(note);
    }
    }
    }

      

    /**
    * 老板
    */
    public class Boss extends Leader {

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

    @Override
    void HandleLeaveNotes(LeaveNote note) {
    System.out.println("批准假条: "+ note.getName());
    }
    }

      

    public class Client {
        public static void main(String[] args) {
            //
            LeaveNote note = new LeaveNote();
            note.setDays(5);
    //        note.setDays(10);
    //        note.setDays(20);
            
            note.setName("tom");
    
            Leader a = new DepartManager("部门领导");
            Leader b = new Manager("总经理");
            Leader c = new DepartManager("老板");
    
            //责任链关系
            a.setNext(b);
            b.setNext(c);
            //处理请假业务
            a.HandleLeaveNotes(note);
        }
    }
    

      责任链模式在处理这种多级业务时可以很好使代码解耦。想新增责任处理对象时只需要增加责任处理对象,不需要更改原来的业务员代码,更加不需要写if..else.

  • 相关阅读:
    Java实现 洛谷 P1060 开心的金明
    (Java实现) 洛谷 P1605 迷宫
    (Java实现) 洛谷 P1605 迷宫
    (Java实现)洛谷 P1093 奖学金
    (Java实现)洛谷 P1093 奖学金
    Java实现 洛谷 P1064 金明的预算方案
    Java实现 洛谷 P1064 金明的预算方案
    (Java实现) 洛谷 P1031 均分纸牌
    QT树莓派交叉编译环开发环境搭建(附多个exe工具下载链接)
    武则天红人对唐睿宗的桃色报复(如此缺少城府,注定了要在宫廷中过早地出局)
  • 原文地址:https://www.cnblogs.com/gcm688/p/10269092.html
Copyright © 2011-2022 走看看