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.

  • 相关阅读:
    XML学习教程
    JSON 的含义?
    局域网共享问题全方位解决
    VMware虚拟化培训手册
    信息化建设中的IT规划精要
    洛谷1265 公路修建
    洛谷1144 最短路计数
    NOI题库05 派
    NOI题库7624 山区建小学(162:Post Office / IOI2000 POST OFFICE [input] )
    vijosP1014 旅行商简化版
  • 原文地址:https://www.cnblogs.com/gcm688/p/10269092.html
Copyright © 2011-2022 走看看