? **定義**: 使多個對象都有機會處理請求,從而避免了發送者和接收者之間的耦合關系。
將這些對象連成一條鏈,并沿著這條連傳遞該請求,直到有對象處理該請求為止。
**一個例子**:
古代女子講究“三從四德”,當女子去做一件事情時,如果該女子未出嫁,首先要向父親請求。
出嫁,就要像丈夫請求;夫死,就要向兒子請求。模擬這個女子發送請求的鏈式關系。

**抽象的處理者實現三個職責:**
---|定義一個請求的處理方法handlerMessage,唯一開發的方法。
---|定義一個編排方法setNext,設置下一個處理者。
---|定義了具體的請求者必須實現的方法。
**處理者中完成的操作**
---|定義自身的責任等級
---|實現抽象方法,對請求者進行封裝,并作出響應。
**請求者完成的操作**
---|負責定義自身的請求等級。
---|發出請求實體。
~~~
public class ResponseTest {
public static void main(String[] args) {
//創建一個女子,發送自己的類型與請求
IWoman woman = new Woman(2,"我想出去購物..");
//創建責任鏈
ResponseHandler farther = new Farther();
ResponseHandler husband = new Husband();
ResponseHandler son = new Son();
//設置下一責任人。
farther.setNext(husband);
husband.setNext(son);
//設置責任鏈入口,處理內容
farther.handleMessage(woman);
}
}
interface IWoman{
//女子發送請求。
String sendRequire();
//獲取當前狀態,出嫁、未嫁、夫死。
int getType();
}
class Woman implements IWoman{
/**
* 1、表示未嫁
* 2、出嫁
* 3、夫死
*/
private int type = 0;
private String request;
//構造函數,設置女子的信息。
public Woman(int type,String require) {
this.type = type;
this.request = require;
}
//獲得請求內容
@Override
public String sendRequire() {
return this.request;
}
//獲取該女子當前的狀態。
@Override
public int getType() {
System.out.println("-------"+this.type);
return this.type;
}
}
//責任鏈接口,定義一些方法,讓子類去實現。
abstract class ResponseHandler{
//父親處理等級
public static final int FARTHER_LEVEL_RESPONSE = 1;
//丈夫處理等級
public static final int HUSBAND_LEVEL_RESPONSE = 2;
//兒子處理等級
public static final int SON_LEVEL_RESPONSE = 3;
//當前等級
private int level = 0;
//設置下一個責任人是誰。
private ResponseHandler nextHandler;
public ResponseHandler(int level) {
this.level = level;
}
public void setNext(ResponseHandler handler){
this.nextHandler = handler;
}
public void handleMessage(IWoman woman){
System.out.println(this.level+"!!!!!!!-----!!!!!"+woman.getType());
if(this.level == woman.getType()){
//如果女子的當前等級與定義的等級相同,那么就做出回應
this.response(woman);
}else{
//不滿足當前等級,尋找下一個責任人
if(nextHandler != null){
//類似于 遞歸回調。
this.nextHandler.response(woman);
}else{ //沒有后繼責任人,就停止回調。
System.out.println("----------找不到責任人了,你自由了....");
}
}
}
//回應方式,具體實現由子類來實現。
protected abstract void response(IWoman woman);
}
/**
*
* @author admin
*
*/
class Farther extends ResponseHandler{
public Farther() {
super(ResponseHandler.FARTHER_LEVEL_RESPONSE);
}
@Override
public void response(IWoman woman) {
System.out.println("----------女兒向父親請求-------");
System.out.println("------"+woman.sendRequire());
System.out.println("我是父親,我統一你的請求...");
}
}
class Husband extends ResponseHandler{
public Husband() {
super(ResponseHandler.HUSBAND_LEVEL_RESPONSE);
}
@Override
public void response(IWoman woman) {
System.out.println("----------妻子向丈夫請求-------");
System.out.println("------"+woman.sendRequire());
System.out.println("我是丈夫,我限制你的自由....");
}
}
class Son extends ResponseHandler{
public Son() {
super(ResponseHandler.SON_LEVEL_RESPONSE);
}
@Override
public void response(IWoman woman) {
System.out.println("----------母親向兒子請求-------");
System.out.println("------"+woman.sendRequire());
System.out.println("我是兒子,我一切都聽你的....");
}
}
~~~
**一個例子**
用戶注冊信息,用戶分為普通用戶和VIP用戶。但是同時公用一個界面。
我們需要一個處理者,來區分不同的注冊用戶。這里可以用到責任鏈模式,來完成。

~~~
public class ResponseEx {
public static void main(String[] args) {
//定義一些登錄者
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("lzl", 1);
hashMap.put("xy", 2);
hashMap.put("ht", 2);
hashMap.put("hj", 2);
hashMap.put("zl",1);
//創建登錄
Registe register = new Registe();
register.setHashMap(hashMap);
//通過Handler處理登錄信息,并通過責任鏈入口、
RegisteHandler CommonHandler = new CommonRegiste();
RegisteHandler VIPHandler = new VIPRegiste();
//設置下一個責任人..
CommonHandler.setHandler(VIPHandler);
CommonHandler.HandleMessage(hashMap);
}
}
abstract class RegisteHandler{
//VIP等級用戶注冊
public static final int VIP_LEVEL = 2;
//普通用戶注冊
public static final int COMMON_LEVEL = 1;
//設置hashMap存儲登錄信息
private HashMap<String, Integer> infoMap = new HashMap<String, Integer>();
//定義當前的等級
private int level ;
//定義下一個責任鏈
private RegisteHandler nextHandler;
//構造方法,設置責任人等級。
public RegisteHandler(int level){
this.level = level;
}
//處理信息
public void HandleMessage(HashMap<String, Integer> hashInfo){
//遍歷hash表中的所用信息。
Set<String> set = hashInfo.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String name = it.next();
//通過name獲得 他的注冊類型。
int type = hashInfo.get(name);
if(this.level == type){
this.infoMap.clear();
this.infoMap.put(name, type);
System.out.println("---------普通用戶--------------");
//如果當前類型與注冊類型相同,則執行方法。
this.response(infoMap);
}else{
//如果沒有找到責任人,繼續查找
if(nextHandler != null){
this.infoMap.clear();
this.infoMap.put(name, type);
System.out.println("---------VIP用戶--------------");
this.nextHandler.response(infoMap);
}else{
//如果找不到責任人。
System.out.println("沒有您選擇的注冊類型,請重新注冊....");
}
}
}
}
public void setHandler(RegisteHandler handler){
this.nextHandler = handler;
}
//定義請求的信息。
protected abstract void response(HashMap<String, Integer> hashMap);
}
class CommonRegiste extends RegisteHandler{
//構造函數設置普通用戶的等級。
public CommonRegiste() {
super(RegisteHandler.COMMON_LEVEL);
}
@Override
protected void response(HashMap<String, Integer> hashMap) {
Set<Map.Entry<String, Integer>> set = hashMap.entrySet();
Iterator<Map.Entry<String, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String, Integer> map = it.next();
String name = map.getKey();
System.out.println("普通用戶:"+name+"\t注冊成功!");
}
}
}
class VIPRegiste extends RegisteHandler{
//構造函數設置普通用戶的等級。
public VIPRegiste() {
super(RegisteHandler.VIP_LEVEL);
}
@Override
protected void response(HashMap<String, Integer> hashMap) {
Set<Map.Entry<String, Integer>> set = hashMap.entrySet();
Iterator<Map.Entry<String, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String, Integer> map = it.next();
String name = map.getKey();
System.out.println("VIP用戶: "+name+"\t注冊成功!");
}
}
}
class Registe{
/**
* 1 表示普通用戶
* 2 表示VIP用戶
*/
private HashMap<String, Integer> hashInfo = new HashMap<String, Integer>();
public void setHashMap(HashMap<String, Integer> hashMap){
this.hashInfo = hashMap;
}
public HashMap<String, Integer> getHashInfo() {
return this.hashInfo;
}
}
~~~
責任鏈式的優點:
1、請求和處理分開,請求者可以不用知道是誰處理,處理者可以不用知道請求的全貌。
2、兩者解耦,提高系統的靈活性
責任鏈式的缺點
1、性能問題
2、調試不方便
- 前言
- 6大設計原則(一)---單一職責原則
- 6大設計原則(二)---里氏替換原則
- 6大設計原則(三)---依賴倒置原則
- 6大設計模式(四)----接口隔離原則
- 6大設計原則(五)---迪米特法則
- 6大設計原則(六)---開閉原則。
- 設計模式(一)---單例模式
- 設計模式(二)---工廠方法模式
- 設計模式(三)---抽象工廠模式
- 設計模式(四)---模板方法模式
- 設計模式(五)---建造者模式
- 設計模式(六)---代理模式
- 設計模式(七)---原型模式
- 設計模式(八)---中介者模式
- 設計模式(九)---命令模式
- 設計模式(十)---責任鏈模式
- 設計模式(十一)---裝飾模式
- 設計模式(十二)---策略模式
- 設計模式(十三)---適配器模式
- 設計模式(十四)---迭代器模式
- 設計模式(十五)---組合模式
- 設計模式(十六)---觀察者模式
- 設計模式(十七)---門面模式
- 設計模式(十八)---備忘錄模式
- 設計模式(十八)---訪問者模式
- 設計模式(二十)---狀態模式
- 設計模式(二十二)---享元模式
- 設計模式(二十三)---橋梁模式