使多個對象都有機會處理請求,避免請求發送者和接受者之間的耦合關係,將接收者對象組成一條鏈,並沿着這條鏈處理請求,直處處理 請求中的某個接受者返回(也包含無下一個接受者的狀況)
假設有一個根據不一樣等級的會員發送卡券包的需求
將請求和處理分開,請求無須知道處理的細節,處理不用知道請求的全貌,二者解耦,提升功能或系統的靈活性java
可以動態的變動(增長、刪除、改變順序)接受者,易擴展設計模式
public class Member { public Member(String name, MemberLevel memberLevel) { this.name = name; this.memberLevel = memberLevel; } private String name; private MemberLevel memberLevel; public String getName() { return name; } public void setName(String name) { this.name = name; } public MemberLevel getMemberLevel() { return memberLevel; } public void setMemberLevel(MemberLevel memberLevel) { this.memberLevel = memberLevel; } @Override public String toString() { return "Member{" + "name='" + name + '\'' + ", memberLevel=" + memberLevel + '}'; } }
public enum MemberLevel { /** * 鑽石 */ DIAMOND(100), /** * 黃金 */ GOLD(90), /** * 白銀 */ SILVER(80), /** * 青銅 */ BRONZE(70); private Integer level; MemberLevel(int level){ this.level = level; } public int getLevelValue(){ return this.level; } }
public abstract class AbstractCouponAssistant { protected Integer memberLevel; protected AbstractCouponAssistant nextCouponAssistant; protected void setNext(AbstractCouponAssistant couponAssistant) { this.nextCouponAssistant = couponAssistant; } public void sendCoupon(Member member){ if(member.getMemberLevel().getLevelValue() >= memberLevel){ this.addCouponToPackage(member); } if(nextCouponAssistant != null){ nextCouponAssistant.sendCoupon(member); } } /** * 增長卡券到卡包 * @param member */ protected abstract void addCouponToPackage(Member member) ; }
public class DiamondCouponAssistant extends AbstractCouponAssistant { public DiamondCouponAssistant(Integer memberLevel) { this.memberLevel = memberLevel; } @Override protected void addCouponToPackage(Member member) { System.out.println("爲會員 {" + member.toString() + "}增長 鑽石卡券"); } }
public class GoldCouponAssistant extends AbstractCouponAssistant { public GoldCouponAssistant(Integer memberLevel){ this.memberLevel = memberLevel; } @Override protected void addCouponToPackage(Member member) { System.out.println("爲會員 {" + member.toString() + "}增長 黃金卡券"); } }
public class SilverCouponAssistant extends AbstractCouponAssistant { public SilverCouponAssistant(Integer memberLevel) { this.memberLevel = memberLevel; } @Override protected void addCouponToPackage(Member member) { System.out.println("爲會員 {" + member.toString() + "}增長 白銀卡券"); } }
public class BronzeCouponAssistant extends AbstractCouponAssistant { public BronzeCouponAssistant(Integer memberLevel) { this.memberLevel = memberLevel; } @Override protected void addCouponToPackage(Member member) { System.out.println("爲會員 {" + member.toString() + "}增長 青銅卡券"); } }
public class ChainPatternDenoClient { public static void main(String[] args) { AbstractCouponAssistant couponAssistant = getCouponAssistantChain(); Member diamondMember = new Member("鑽石王", MemberLevel.DIAMOND); Member goldMember = new Member("黃金張", MemberLevel.GOLD); Member silverMember = new Member("白銀黃", MemberLevel.SILVER); Member bronzeMember = new Member("青銅吳", MemberLevel.BRONZE); System.out.println("開始發送卡券"); couponAssistant.sendCoupon(diamondMember); System.out.println("=====發送下一個===="); couponAssistant.sendCoupon(goldMember); System.out.println("=====發送下一個===="); couponAssistant.sendCoupon(silverMember); System.out.println("=====發送下一個===="); couponAssistant.sendCoupon(bronzeMember); } private static AbstractCouponAssistant getCouponAssistantChain() { DiamondCouponAssistant diamondCouponAssistant = new DiamondCouponAssistant(MemberLevel.DIAMOND.getLevelValue()); GoldCouponAssistant goldCouponAssistant = new GoldCouponAssistant(MemberLevel.GOLD.getLevelValue()); SilverCouponAssistant silverCouponAssistant = new SilverCouponAssistant(MemberLevel.SILVER.getLevelValue()); BronzeCouponAssistant bronzeCouponAssistant = new BronzeCouponAssistant(MemberLevel.BRONZE.getLevelValue()); diamondCouponAssistant.setNext(goldCouponAssistant); goldCouponAssistant.setNext(silverCouponAssistant); silverCouponAssistant.setNext(bronzeCouponAssistant); return diamondCouponAssistant; } }
控制責任鏈的長度,通常在抽象的的接受者設置一個閾值,執行setNext時檢查是否超出閾值,以確保鏈條的長度再可控範圍內