责任链模式

责任链模式要达到的目的:责任链模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求

具体实例


Application
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package zherenlian;

/**
* Created by 15581 on 2017/11/8.
*/ // Application.java
public class Application{
private Handler beijing,shanghai,tianjin; //责任链上的对象
public void createChain(){ //建立责任链
beijing=new Beijing();
shanghai=new Shanghai();
tianjin=new Tianjin();
beijing.setNextHandler(shanghai);
shanghai.setNextHandler(tianjin);
}
public void reponseClient(String number){ //响应用户的请求
beijing.handleRequest(number);
}
public static void main(String args[]){
Application application=new Application();
application.createChain();
application.reponseClient("77720810340930632");;
}
}

Beijing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

package zherenlian;

import java.util.ArrayList;

/**
* Created by 15581 on 2017/11/8.
*/
public class Beijing implements Handler{
private Handler handler; //存放当前处理者后继的Hander接口变量
private ArrayList<String> numberList; //存放身份证号码的数组线性表
Beijing(){
numberList=new ArrayList<String>();
numberList.add("11129812340930034"); //这里使用模拟的身份证号码
numberList.add("10120810340930632");
numberList.add("22029812340930034");
numberList.add("32620810340930632");
}
public void handleRequest(String number){
if(numberList.contains(number))
System.out.println("该人在北京居住");
else{
System.out.println("该人不在北京居住");
if(handler!=null)
handler.handleRequest(number); //将请求传递给下一个处理者
}
}
public void setNextHandler(Handler handler){
this.handler=handler;
}
}

Handler

1
2
3
4
5
6
7
package zherenlian;

// Handler.java
public interface Handler{
public abstract void handleRequest(String number);
public abstract void setNextHandler(Handler handler);
}

Shanghai

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package zherenlian;

import java.util.ArrayList;

/**
* Created by 15581 on 2017/11/8.
*/
public class Shanghai implements Handler{
private Handler handler; //存放当前处理者后继的Hander接口变量
private ArrayList<String> numberList; //存放身份证号码的数组线性表
Shanghai(){
numberList=new ArrayList<String>();
numberList.add("34529812340930034"); //这里使用模拟的身份证号码
numberList.add("98720810340430632");
numberList.add("36529812340930034");
numberList.add("77720810340930632");
}
public void handleRequest(String number){
if(numberList.contains(number))
System.out.println("该人在上海居住");
else{
System.out.println("该人不在上海居住");
if(handler!=null)
handler.handleRequest(number); //将请求传递给下一个处理者
}
}
public void setNextHandler(Handler handler){
this.handler=handler;
}
}

Tianjin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package zherenlian;

import java.util.ArrayList;

/**
* Created by 15581 on 2017/11/8.
*/
public class Tianjin implements Handler{
private Handler handler; //存放当前处理者后继的Hander接口变量
private ArrayList<String> numberList; //存放身份证号码的数组线性表
Tianjin(){
numberList=new ArrayList<String>();
numberList.add("10029812340930034"); //这里使用模拟的身份证号码
numberList.add("20020810340430632");
numberList.add("30029812340930034");
numberList.add("50020810340930632");
}
public void handleRequest(String number){
if(numberList.contains(number))
System.out.println("该人在天津居住");
else{
System.out.println("该人不在天津居住");
if(handler!=null)
handler.handleRequest(number); //将请求传递给下一个处理者
}
}
public void setNextHandler(Handler handler){
this.handler=handler;
}
}