博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习笔记——责任链模式
阅读量:5878 次
发布时间:2019-06-19

本文共 2776 字,大约阅读时间需要 9 分钟。

模拟实现类:

1 /** 2  *责任链模式: 将处理同一类事务的所有对象按次序连接在一起(就像连成一条锁链一样), 3  * 依次去处理待定事务,如果该对象处理不了,就传给其后的对象来处理 4  */ 5 public abstract class Leader { 6     public Leader(String  name, Leader nextLeader){ 7         this.name = name; 8         this.nextLeader = nextLeader; 9     }10 11     public void setNextLeader(Leader nextLeader){12         this.nextLeader = nextLeader;13     }14 15     public abstract void handleRequest(LeaveRequest request);16 17     protected String name;18     protected Leader nextLeader;19 }20 21 class Director extends Leader{22     public Director(String name, Leader nextLeader){23         super(name, nextLeader);24     }25 26     @Override27     public void handleRequest(LeaveRequest request) {28         if(request.getDay() <= 5){29             System.out.println(name + "Director: OK!");30         } else{31             if(nextLeader != null){32                 nextLeader.handleRequest(request);33             }34         }35     }36 }37 38 class Manager extends Leader{39     public Manager(String name, Leader nextLeader){40         super(name, nextLeader);41     }42 43     @Override44     public void handleRequest(LeaveRequest request) {45         if(request.getDay() <= 10){46             System.out.println(name + "Manager: OK!");47         } else{48             if(nextLeader != null){49                 nextLeader.handleRequest(request);50             }51         }52     }53 }54 55 class GeneralManager extends Leader{56     public GeneralManager(String name){57         super(name, null);58     }59 60     @Override61     public void handleRequest(LeaveRequest request) {62         System.out.println(name + "GeneralManager: OK!");63     }64 }
View Code
1 public class LeaveRequest { 2     public LeaveRequest(String name, int day, String reason){ 3         this.name = name; 4         this.day = day; 5         this.reason = reason; 6     } 7  8     public String getName() { 9         return name;10     }11 12     public void setName(String name) {13         this.name = name;14     }15 16     public int getDay() {17         return day;18     }19 20     public void setDay(int day) {21         this.day = day;22     }23 24     public String getReason() {25         return reason;26     }27 28     public void setReason(String reason) {29         this.reason = reason;30     }31 32     private String name;33     private int day;34     private String reason;35 }
View Code

测试类:

1 public class Test{2     public static void main(String[] args){3         Leader leader1 = new GeneralManager("张三");4         Leader leader2 = new Manager("李四", leader1);5         Leader leader3 = new Director("王五", leader2);6         leader3.handleRequest(new LeaveRequest("小李", 7, "结婚"));7     }8 }
View Code

 

转载于:https://www.cnblogs.com/Hr666/p/10384710.html

你可能感兴趣的文章
Entity Framework Code First 模式-建立多对多联系
查看>>
[LeetCode] Reverse Lists
查看>>
前台页面之<base>标签
查看>>
angular分页插件tm.pagination 解决触发二次请求的问题
查看>>
day08-文件操作
查看>>
教学-45 对象的相等
查看>>
贪食蛇
查看>>
关于Spring 中的事务
查看>>
为什么现在都用面向对象开发,为什么现在都用分层开发结构?
查看>>
【离散数学】 SDUT OJ 偏序关系
查看>>
写给学弟学妹的产品入门建议(持续更新)
查看>>
view视图总结
查看>>
oracle11g 数据库导出报“ EXP-00003:
查看>>
201521123009 《Java程序设计》第11周学习总结
查看>>
可解释的机器学习
查看>>
Python3之多线程学习
查看>>
aspx页面@Page指令解析
查看>>
POJ 2002
查看>>
MVC和MTV结构分析
查看>>
(转)微信网页扫码登录的实现
查看>>