1. 程式人生 > >責任鏈模式Chain of Responsibility

責任鏈模式Chain of Responsibility

完整設計模式目錄見:https://blog.csdn.net/u013523089/article/details/82852049

責任鏈模式:存在層級責任關係(省長–>市長–>區長–>旗/縣長);每一級可以選擇自己處理還是交由下一級處理;客戶端需要指定層級關係,可以直接指定從哪個位置開始

比如有如下場景:
層級關係為:father–>husband–>son
兩種形式的場景
1.woman要逛街,需要先找father,father接到請求,同意,與father一起去結束
2.woman要逛街,需要先找father,father同意後,找下一級即husband,husband同意後,一起去結束

在這裡插入圖片描述

客戶端操作

package com.zhaowd.test.designPattern.chainOfResponsibility責任鏈模式;

public class Test {
	
	/**
	 * 責任鏈模式:層級關係,第一級選擇是否自己處理還是交由下一級處理
	 * 應用也可以是選擇,例:根據引數來選擇是第幾級處理,也可以視為第一級轉下一級處理
	 * @param args
	 */
	public static void main(String[] args) {
		IWoman woman = new Woman("0", "逛街");
		
		Man father = new Father();
		Husband husband = new Husband();
		
		father.setNextMan(husband);
		father.dealRequest(woman);
	}
}