• 设计模式Design Pattern(3) 责任链模式


    什么是责任链模式?

    责任链模式(Chain of Responsibility Pattern):请求知道公开接口,但不知道那个具体类处理,这些具体处理类对象连接成一条链。请求沿着这条链传递,直到有对象处理它为止。

    解决什么问题?

    职责链将请求的发送者和请求的处理者解耦,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递。

    处理原则 "如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。"

    场景应用

    以医院看病为例

    病人类

     1 /**
     2  * 病人
     3  */
     4 public class Patient {
     5     //病人名字
     6     String name ;
     7     //病人年龄
     8     int age;
     9     //病例描述
    10     String describe;
    11     //病人状态
    12     String state;
    13 
    14     public String getState() {
    15         return state;
    16     }
    17 
    18     public void setState(String state) {
    19         this.state = state;
    20     }
    21 
    22     public String getName() {
    23         return name;
    24     }
    25 
    26     public void setName(String name) {
    27         this.name = name;
    28     }
    29 
    30     public int getAge() {
    31         return age;
    32     }
    33 
    34     public void setAge(int age) {
    35         this.age = age;
    36     }
    37 
    38     public String getDescribe() {
    39         return describe;
    40     }
    41 
    42     public void setDescribe(String describe) {
    43         this.describe = describe;
    44     }
    45 }
    Patient.java

    医院类

     1 import java.util.ArrayList;
     2 import java.util.List;
     3 
     4 /**
     5  * 医院
     6  */
     7 public class Hospital {
     8 
     9     /**
    10      * 病人
    11      */
    12     private Patient patient;
    13 
    14     /**
    15      * 责任链
    16      */
    17     public List<Department> chain = new ArrayList<>();
    18 
    19     public Hospital() {
    20         InitDepartmentChain();
    21     }
    22 
    23     /**
    24      * 初始化科室
    25      */
    26     private void InitDepartmentChain() {
    27         HeadDepartment head = new HeadDepartment();
    28         chain.add(head);
    29         HandDepartment hand = new HandDepartment();
    30         chain.add(hand);
    31         FootDepartment foot = new FootDepartment();
    32         chain.add(foot);
    33     }
    34 
    35     /**
    36      * 接收病人
    37      *
    38      * @param p 病人
    39      */
    40     public void receivePatien(Patient p) {
    41         this.patient = p;
    42         this.patient=treatment();
    43         if ("痊愈".equals(this.patient.state)){
    44             System.out.println("恭喜病人已经痊愈!可以出院了!!!");
    45         }else{
    46             System.out.println("sorry!本医院无法治疗该疑难杂症,请转到别的医院!");
    47         }
    48     }
    49 
    50     /**
    51      * 治疗病人
    52      *
    53      * @return 病人
    54      */
    55     public Patient treatment() {
    56         for (Department department : chain) {
    57             patient =department.receivePatient(patient);
    58             if("痊愈".equals(patient.state))
    59                 break;
    60         }
    61         return patient;
    62     }
    63 }
    Hospital.java

    医院科室基类

     1 /**
     2  * 科室
     3  */
     4 public abstract class Department {
     5 
     6     //科室名称
     7     String name;
     8 
     9     public String getName() {
    10         return name;
    11     }
    12 
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16 
    17     //职能
    18     String duty;
    19 
    20     public String getDuty() {
    21         return duty;
    22     }
    23 
    24     public void setDuty(String duty) {
    25         this.duty = duty;
    26     }
    27 
    28     //病人
    29     Patient patient;
    30 
    31     public Patient receivePatient(Patient p) {
    32         System.out.println("欢迎来到科室:" + this.name);
    33         this.patient = p;
    34         return treatment();
    35     }
    36 
    37     //判断是否是科室范畴病人
    38     private boolean isMyPatient() {
    39         return this.patient != null && this.patient.describe.equals(duty);
    40     }
    41 
    42     private Patient treatment() {
    43         if (isMyPatient()) {
    44             System.out.println("科室[" + this.name + "]接收了病人");
    45             System.out.println("病人资料\n\t" + "名字:" + this.patient.name + "\n\t年龄:" + this.patient.age + "\n\t病例:" + this.patient.describe);
    46             System.out.println("开始治疗病人。。。。。");
    47             this.patient.state = "痊愈";
    48             System.out.println("病人已经医治好");
    49         } else {
    50             System.out.println("sorry,不是科室[" + this.name + "]的范畴,请到别的科室诊治!");
    51         }
    52 
    53         return this.patient;
    54     }
    55 
    56 }
    Department.java

    头部科室类

     1 /**
     2  * 头部门诊
     3  */
     4 public class HeadDepartment extends Department {
     5 
     6     public HeadDepartment(){
     7         this.setName("头部门诊");
     8         this.setDuty("头痛");
     9     }
    10 }
    HeadDepartment.java

    手部科室类

    1 /**
    2  * 手科室
    3  */
    4 public class HandDepartment extends Department {
    5     public HandDepartment(){
    6         this.setName("手部科室");
    7         this.setDuty("手痛");
    8     }
    9 }
    HandDepartment.java

    脚部科室类

    1 /**
    2  * 脚部科室
    3  */
    4 public class FootDepartment extends Department {
    5     public FootDepartment(){
    6         this.setName("脚部科室");
    7         this.setDuty("脚痛");
    8     }
    9 }
    FootDepartment.java

    main方法

     1 public class App {
     2     public static void main(String[] args) {
     3         Hospital hospital = new Hospital();
     4         Patient patient = new Patient();
     5         patient.setName("王五");
     6         patient.setAge(12);
     7         patient.setDescribe("手痛");
     8         patient.setState("生病了");
     9         hospital.receivePatien(patient);
    10     }
    11 }
    main方法

    输出结果

    欢迎来到科室:头部门诊
    sorry,不是科室[头部门诊]的范畴,请到别的科室诊治!
    欢迎来到科室:手部科室
    科室[手部科室]接收了病人
    病人资料
        名字:王五
        年龄:12
        病例:手痛
    开始治疗病人。。。。。
    病人已经医治好
    恭喜病人已经痊愈!可以出院了!!!

    总结

    (1)消息处理者有多个,他们构成一条责任链,请求只管把请求发送到责任链上,却不关心处理细节和处理传递。

    (2)沿着这条链传递请求,直到有对象处理它为止或整条责任链传递完成。

     实例源码:https://github.com/LF20160912/pattern

    如果该文章对你有所帮助,请点个赞支持下,谢谢!
  • 相关阅读:
    一个封装好的滚动js类 效果很多 兼容也比较好
    a标签超链接点击后无虚边框
    添加测试数据时的优化
    select 友情链接
    jquery 团购倒计时
    [JZOJ 5129] 字符串
    [JZOJ 5810] 简单的玄学
    [JZOJ 100025] 棋盘
    [JZOJ 5600] Arg
    [luogu 4389] 付公主的背包
  • 原文地址:https://www.cnblogs.com/lfhappy/p/10967584.html
Copyright © 2020-2023  润新知