Java 設計模式之責任鏈模式及異步責任鏈詳解
一、定義
責任鏈模式(Chain of Responsibility Pattern):避免將一個請求的發送者與接受者耦合在一起,讓多個對象都有機會處理請求。將接受請求的對象連接成一條鏈,並且沿著這條鏈傳遞請求,直到有一個對象能夠處理它為止。
在很多源碼都有涉及,如Mybatis攔截器、Filter…
責任鏈模式屬於行為型模式。
二、普通責任鏈模式
抽象處理類:AbstractProcessor
/** * 抽象處理類 */ public abstract class AbstractProcessor { // 責任鏈 private AbstractProcessor nextProcessor; public void setNextProcessor(AbstractProcessor nextProcessor){ this.nextProcessor = nextProcessor; } /** * 鏈路調用 * @param obj */ public void execute(Object obj) { this.processor(obj); if (this.nextProcessor != null){ this.nextProcessor.execute(obj); } } public abstract void processor(Object obj); }
具體類:ParamsProcessor、CheckProcessor、FinalProcessor
public class ParamsProcessor extends AbstractProcessor{ @Override public void processor(Object obj) { System.out.println("paramsProcessor>>>>>>>>>>" + obj.toString()); } } public class CheckProcessor extends AbstractProcessor{ @Override public void processor(Object obj) { System.out.println("checkProcessor>>>>>>>>>>" + obj.toString()); } } public class FinalProcessor extends AbstractProcessor{ @Override public void processor(Object obj) { System.out.println("finalProcessor>>>>>>>>>>" + obj.toString()); } }
測試類:ProcessorMainTest
public class ProcessorMainTest { public static void main(String[] args) { /** * 構建一個鏈路 * paramsProcessor -> checkProcessor -> finalProcessor */ AbstractProcessor paramsProcessor = new ParamsProcessor(); AbstractProcessor checkProcessor = new CheckProcessor(); AbstractProcessor finalProcessor = new FinalProcessor(); paramsProcessor.setNextProcessor(checkProcessor); checkProcessor.setNextProcessor(finalProcessor); paramsProcessor.execute("Java責任鏈模式"); } }
執行結果
paramsProcessor>>>>>>>>>>Java責任鏈模式
checkProcessor>>>>>>>>>>Java責任鏈模式
finalProcessor>>>>>>>>>>Java責任鏈模式
三、異步責任鏈模式
異步責任鏈模式采用責任鏈模式、生產者消費者模式組合。
抽象處理類:AbstractAsynProcessor
/** * 異步責任鏈:采用線程、生產者消費者模式實現 */ public abstract class AbstractAsynProcessor extends Thread { // 用於存放當前責任鏈需要處理的對象 protected BlockingQueue<Object> queue; // 鏈路對象 private AbstractAsynProcessor nextProcessor; /** * 執行 * * @param obj */ public void execute(Object obj) throws InterruptedException { this.processor(obj); if (this.nextProcessor != null) { this.nextProcessor.getQueue().put(obj); } } @Override public void run() { // 執行 try { while (true) { // 生產者消費者 Object obj = this.queue.take(); execute(obj); } } catch (InterruptedException e) { e.printStackTrace(); } } public abstract void processor(Object obj); public BlockingQueue<Object> getQueue() { return queue; } public void setNextProcessor(AbstractAsynProcessor nextProcessor) { this.nextProcessor = nextProcessor; } }
具體類:AsynParamsProcessor、AsynCheckProcessor、AsynFinalProcessor
public class AsynParamsProcessor extends AbstractAsynProcessor { public AsynParamsProcessor() { // 初始化隊列 this.queue = new LinkedBlockingQueue<>(); } @Override public void processor(Object obj) { System.out.println("asynParamsProcessor>>>>>>>>>>" + obj.toString()); } } public class AsynCheckProcessor extends AbstractAsynProcessor { public AsynCheckProcessor(){ this.queue = new LinkedBlockingQueue<>(); } @Override public void processor(Object obj) { System.out.println("asynCheckProcessor>>>>>>>>>>" + obj.toString()); } } public class AsynFinalProcessor extends AbstractAsynProcessor { public AsynFinalProcessor() { this.queue = new LinkedBlockingQueue<>(); } @Override public void processor(Object obj) { System.out.println("asynFinalProcessor>>>>>>>>>>" + obj.toString()); } }
測試類:AsynProcessorMainTest
public class AsynProcessorMainTest { public static void main(String[] args) throws InterruptedException { /** * 構建一個鏈路 * asynParamsProcessor -> asynCheckProcessor -> asynFinalProcessor */ AbstractAsynProcessor asynParamsProcessor = new AsynParamsProcessor(); asynParamsProcessor.start(); AbstractAsynProcessor asynCheckProcessor = new AsynCheckProcessor(); asynCheckProcessor.start(); AbstractAsynProcessor asynFinalProcessor = new AsynFinalProcessor(); asynFinalProcessor.start(); asynParamsProcessor.setNextProcessor(asynCheckProcessor); asynCheckProcessor.setNextProcessor(asynFinalProcessor); Scanner input = new Scanner(System.in); while (true) { Thread.sleep(1000); System.out.print("請輸入:"); asynParamsProcessor.getQueue().put(input.next()); } } }
執行結果
請輸入:Java異步責任鏈
asynParamsProcessor>>>>>>>>>>Java異步責任鏈
asynCheckProcessor>>>>>>>>>>Java異步責任鏈
asynFinalProcessor>>>>>>>>>>Java異步責任鏈
到此這篇關於Java 設計模式之責任鏈模式及異步責任鏈詳解的文章就介紹到這瞭,更多相關Java 設計模式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java簡單實現定時器
- 一篇文章帶你深入瞭解Java對象與Java類
- Java 線程相關總結
- Java 面向對象之繼承篇詳解原理與特點
- java並發編程工具類JUC之ArrayBlockingQueue