一、前言
责任链模式将请求的处理类链接在一起,请求发送方不需要关心谁处理这个请求,接受方也不需要知道目前这个请求是谁发出的,在处理这个请求的时候,接受者根据职责链依次往下寻找能处理该请求的类,直到找到为止。这种设计模式将请求者和接受者进行了解耦。
二、职责链模式
概述: 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链, 并沿着这条链传递该请求,直到有一个对象处理它为止。
适用性:
1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
2、你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
3、可处理一个请求的对象集合应被动态指定。
这种设计模式降低了请求发送者和接受者的耦合度,使得我们需要再增加职责链时会显得很方便,增加了代码的灵活度,但是这种设计模式并不能保证我们的发送方发送的请求就一定会处理,而且 在处理请求时会有一个寻找处理者的过程,会降低系统的性能。
三、代码展示
通过病人看病的例子来展示责任链模式
3.1 病人 和医生 抽象类
public abstract class Doctor {
public abstract void diagnosis(Patient patient);
}
public abstract class Patient {
public abstract void illness();
}
3.2 实现类
public class DentalPatients extends Patient{
@Override
public void illness() {
// TODO Auto-generated method stub
System.out.println("症状:牙痛");
}
}
public class MedicalPatient extends Patient{
@Override
public void illness() {
// TODO Auto-generated method stub
System.out.println("症状:肚子痛");
}
}
public class SkinPatients extends Patient{
@Override
public void illness() {
// TODO Auto-generated method stub
System.out.println("症状: 皮肤过敏");
}
}
public class Dentist extends Doctor{
private Doctor nexDoctor;
@Override
public void diagnosis(Patient patient) {
// TODO Auto-generated method stub]
patient.illness();
if(patient instanceof DentalPatients)
{
System.out.println("我是牙科医生,我可以治疗");
}
else
{
System.out.println("我是牙科医生,我不可以治疗");
nexDoctor.diagnosis(patient);
}
}
public Doctor getNexDoctor() {
return nexDoctor;
}
public void setNexDoctor(Doctor nexDoctor) {
this.nexDoctor = nexDoctor;
}
}
public class SkinDoctor extends Doctor{
private Doctor nexDoctor;
@Override
public void diagnosis(Patient patient) {
// TODO Auto-generated method stub
patient.illness();
if(patient instanceof SkinPatients)
{
System.out.println("我是皮肤科医生,我可以治疗");
}
else
{
System.out.println("我是皮肤科医生,我不可以治疗");
nexDoctor.diagnosis(patient);
}
}
public Doctor getNexDoctor() {
return nexDoctor;
}
public void setNexDoctor(Doctor nexDoctor) {
this.nexDoctor = nexDoctor;
}
}
public class MedicalDoctor extends Doctor{
private Doctor nexDoctor;
@Override
public void diagnosis(Patient patient) {
// TODO Auto-generated method stub
patient.illness();
if(patient instanceof MedicalPatient)
{
System.out.println("我是内科医生,我可以治疗");
}
else
{
System.out.println("我是内科医生,我不可以治疗");
nexDoctor.diagnosis(patient);
}
}
public Doctor getNexDoctor() {
return nexDoctor;
}
public void setNexDoctor(Doctor nexDoctor) {
this.nexDoctor = nexDoctor;
}
}
3.3 结果
public class Test {
public static void main(String[] args) {
MedicalDoctor medicalDoctor = new MedicalDoctor();
SkinDoctor skinDoctor = new SkinDoctor();
Dentist dentist = new Dentist();
skinDoctor.setNexDoctor(dentist);
medicalDoctor.setNexDoctor(skinDoctor);
Patient patient = new DentalPatients();
Patient patient1 = new SkinPatients();
Patient patient2 = new MedicalPatient();
medicalDoctor.diagnosis(patient);
System.out.println("======================");
medicalDoctor.diagnosis(patient1);
System.out.println("======================");
medicalDoctor.diagnosis(patient2);
}
}
症状:牙痛
我是内科医生,我不可以治疗
症状:牙痛
我是皮肤科医生,我不可以治疗
症状:牙痛
我是牙科医生,我可以治疗
======================
症状: 皮肤过敏
我是内科医生,我不可以治疗
症状: 皮肤过敏
我是皮肤科医生,我可以治疗
======================
症状:肚子痛
我是内科医生,我可以治疗