设计模式之职责链模式笔记

这个模式用的不多,笔记之,病人去医院看病的例子,来自易说设计模式一书

public abstract class Doctor {
    public abstract void seeDoctor(XiaoGong xiaoGong);

    public Doctor getDoctor(){ return doctor; }

    public void setDoctor(Doctor doctor){ this.doctor = doctor; }

    private Doctor doctor;
}

 

public class NerveDoctor extends Doctor {
    public void seeDoctor(XiaoGong xiaoGong) {
        if (xiaoGong.getHead().booleanValue()) {
            System.out.println("神经科的医生看病");
        } else if (super.getDoctor() != null) {
   super.getDoctor().seeDoctor(xiaoGong);
        }
    }
}

public class SleepDoctor extends Doctor {
    public void seeDoctor(XiaoGong xiaoGong) {
        if (xiaoGong.getSleep().booleanValue()) {
            System.out.println("睡眠中心的医生看病");
        } else if (super.getDoctor() != null) {
   super.getDoctor().seeDoctor(xiaoGong);
        }
    }
}

public class AssimilationDoctor extends Doctor {
    public void seeDoctor(XiaoGong xiaoGong) {
        if (xiaoGong.getStomach().booleanValue()) {
            System.out.println("消化科的医生看病");
        } else if (super.getDoctor() != null) {
   super.getDoctor().seeDoctor(xiaoGong);
        }
    }
}

 

病人类
public class XiaoGong {
    public Boolean getSleep(){ return sleep; }

    public void setSleep(Boolean sleep){ this.sleep = sleep; }

    public Boolean getHead(){ return head; }

    public void setHead(Boolean head){ this.head = head; }

    public Boolean getStomach(){ return stomach; }

    public void setStomach(Boolean stomach){ this.stomach = stomach; }

    private Boolean sleep;
    private Boolean head;
    private Boolean stomach;
}

 

客户调用类
 XiaoGong xiaoGong = new XiaoGong();
  Doctor nerveDoctor = new NerveDoctor();
        Doctor sleepDoctor = new SleepDoctor();
        Doctor assimilationDoctor = new AssimilationDoctor();
  nerveDoctor.setDoctor(sleepDoctor);
  sleepDoctor.setDoctor(assimilationDoctor);
      xiaoGong.setHead(new Boolean(true));
  nerveDoctor.seeDoctor(xiaoGong);
        xiaoGong.setSleep(new Boolean(true));
  xiaoGong.setHead(new Boolean(false));
        nerveDoctor.seeDoctor(xiaoGong);
        xiaoGong.setStomach(new Boolean(true));
  xiaoGong.setSleep(new Boolean(false));
        nerveDoctor.seeDoctor(xiaoGong);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值