摘要:實現(xiàn)一經(jīng)典模式該模式特點多線程環(huán)境環(huán)境下,需要每一個客戶端請求,一個對象。代碼測試程序注意該測試程序是有的,多線程共享會導致的游標定位錯誤。
實現(xiàn)一:servlet 經(jīng)典模式
該模式特點:多線程環(huán)境環(huán)境下,需要每一個客戶端請求,new 一個ConcreteFilterChain對象。如果對象太大,增加cpu和gc的負擔。
代碼demoimport java.io.IOException; public interface Filter { public void doFilter(Context ctx, FilterChain chain) throws IOException; }
import java.io.IOException; public interface FilterChain { void doFilter(Context ctx) throws IOException; }
import java.io.IOException; public class PrintOne implements Filter { @Override public void doFilter(Context ctx, FilterChain chain) throws IOException { System.out.println(Thread.currentThread().getId()+ " 1"); chain.doFilter(ctx); } } import java.io.IOException; public class PrintTwo implements Filter { @Override public void doFilter(Context ctx, FilterChain chain) throws IOException { System.out.println(Thread.currentThread().getId()+" 2"); chain.doFilter(ctx); } } import java.io.IOException; public class PrintThree implements Filter { @Override public void doFilter(Context ctx, FilterChain chain) throws IOException { System.out.println(Thread.currentThread().getId()+" 3"); chain.doFilter(ctx); } }
import java.io.IOException; import java.util.List; public class DefaultFilterChain implements FilterChain { public DefaultFilterChain(List測試程序list) { this.list = list; } List list; int pos = 0; @Override public void doFilter(Context ctx) throws IOException { if(pos < list.size()){ Filter filter = list.get(pos); pos++; filter.doFilter(ctx,this); } } }
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestMain { static ExecutorService executorService = Executors.newFixedThreadPool(5); public static void main(String args[]){ Listlist = new ArrayList (); list.add(new PrintOne()); list.add(new PrintTwo()); list.add(new PrintThree()); FilterChain filterChain = new DefaultFilterChain(list); for(int i=0;i<2;i++) { executorService.submit(new Event(filterChain)); } } public static class Event implements Runnable{ public Event(FilterChain filterChain) { this.filterChain = filterChain; } FilterChain filterChain; @Override public void run() { Context ctx = new Context(); try { filterChain.doFilter(ctx); } catch (IOException e) { e.printStackTrace(); } } } }
注意:該測試程序是有bug的,多線程共享filterChain會導致filter的游標定位錯誤。要解決這個問題,要么每個請求new一個DefaultFilterChain對象。
實現(xiàn)二參考netty pipeline的實現(xiàn)
該實現(xiàn)的優(yōu)點:多線程環(huán)境下,不需要每一個客戶的請求都new一個DefaultFilterChain.該算法的巧妙之處在于:將handler跟filterchain 組成成一個節(jié)點。 通過handler驅(qū)動節(jié)點的扭轉(zhuǎn)
上代碼public class Context { } public interface Handler { void handle(Context ctx,FilterChain filterChain); } public class PrintOne implements Handler { @Override public void handle(Context ctx, FilterChain filterChain) { System.out.println(Thread.currentThread().getId()+" 1"); filterChain.fireNext(ctx); } } public class PrintTwo implements Handler{ @Override public void handle(Context ctx, FilterChain filterChain) { System.out.println(Thread.currentThread().getId()+" 2"); filterChain.fireNext(ctx); } } public class PrintThree implements Handler { @Override public void handle(Context ctx, FilterChain filterChain) { System.out.println(Thread.currentThread().getId()+" 3"); filterChain.fireNext(ctx); } }
FilterChain和DefaultFilterChain
public interface FilterChain { void handler(Context ctx); void fireNext(Context ctx); } public class DefaultFilterChain implements FilterChain { private FilterChain next; private Handler handler; public DefaultFilterChain(FilterChain next, Handler handler) { this.next = next; this.handler = handler; } @Override public void handler(Context ctx) { handler.handle(ctx,this); } public void fireNext(Context ctx){ FilterChain next_ = this.next; if(next_ != null){ next_ .handler(ctx); } } }測試代碼
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FilterChainTest { static ExecutorService executorService = Executors.newFixedThreadPool(3); public static void main(String args[]){ Handler h1 = new PrintOne(); Handler h2 = new PrintTwo(); Handler h3 = new PrintThree(); FilterChain filterChain3 = new DefaultFilterChain(null,h3); FilterChain filterChain2 = new DefaultFilterChain(filterChain3,h2); FilterChain filterChain1 = new DefaultFilterChain(filterChain2,h1); Context ctx = new Context(); for(int i=0;i<3;i++){ executorService.execute(new RunPrintEcho(filterChain1,ctx)); } } public static class RunPrintEcho implements Runnable{ FilterChain filterChain; Context ctx; public RunPrintEcho(FilterChain filterChain, Context ctx) { this.filterChain = filterChain; this.ctx = ctx; } @Override public void run() { filterChain.handler(this.ctx); } } }
大功告成!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69201.html
摘要:責任鏈模式的具體運用以及原理請參見筆者責任鏈模式改進方式引入適配器模式關(guān)于接口適配器模式原理以及使用場景請參見筆者適配器模式。 1 責任鏈模式現(xiàn)存缺點 由于責任鏈大多數(shù)都是不純的情況,本案例中,只要校驗失敗就直接返回,不繼續(xù)處理接下去責任鏈中的其他校驗邏輯了,故而出現(xiàn)如果某個部分邏輯是要由多個校驗器組成一個整理的校驗邏輯的話,則此責任鏈模式則顯現(xiàn)出了它的不足之處了。(責任鏈模式的具體運...
摘要:實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點參考設計模式和開發(fā)實踐之責任鏈模式職責鏈模式 作者按:《每天一個設計模式》旨在初步領(lǐng)會設計模式的精髓,目前采用javascript和python兩種語言實現(xiàn)。誠然,每種設計模式都有多種實現(xiàn)方式,但此小冊只記錄最直截了當?shù)膶崿F(xiàn)方式 :) 0. 項目地址 責任鏈模式·代碼 《每天一個設計模式》地址 1. 什么是...
摘要:實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點參考設計模式和開發(fā)實踐之責任鏈模式職責鏈模式 作者按:《每天一個設計模式》旨在初步領(lǐng)會設計模式的精髓,目前采用javascript和python兩種語言實現(xiàn)。誠然,每種設計模式都有多種實現(xiàn)方式,但此小冊只記錄最直截了當?shù)膶崿F(xiàn)方式 :) 0. 項目地址 責任鏈模式·代碼 《每天一個設計模式》地址 1. 什么是...
摘要:實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點參考設計模式和開發(fā)實踐之責任鏈模式職責鏈模式 作者按:《每天一個設計模式》旨在初步領(lǐng)會設計模式的精髓,目前采用javascript和python兩種語言實現(xiàn)。誠然,每種設計模式都有多種實現(xiàn)方式,但此小冊只記錄最直截了當?shù)膶崿F(xiàn)方式 :) 0. 項目地址 責任鏈模式·代碼 《每天一個設計模式》地址 1. 什么是...
摘要:實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點實現(xiàn)以下是測試代碼設置下一個處理的節(jié)點參考設計模式和開發(fā)實踐之責任鏈模式職責鏈模式 作者按:《每天一個設計模式》旨在初步領(lǐng)會設計模式的精髓,目前采用javascript和python兩種語言實現(xiàn)。誠然,每種設計模式都有多種實現(xiàn)方式,但此小冊只記錄最直截了當?shù)膶崿F(xiàn)方式 :) 0. 項目地址 責任鏈模式·代碼 《每天一個設計模式》地址 1. 什么是...
閱讀 2394·2023-04-25 19:27
閱讀 3505·2021-11-24 09:39
閱讀 3919·2021-10-08 10:17
閱讀 3409·2019-08-30 13:48
閱讀 1943·2019-08-29 12:26
閱讀 3132·2019-08-28 17:52
閱讀 3548·2019-08-26 14:01
閱讀 3543·2019-08-26 12:19