1. Definition
Enables multiple objects to have the opportunity to process requests, thereby avoiding the coupling between the sender and receiver of the request. Connect these objects as a chain and pass the request along this chain until an object has processed it.
2. Awareness Map
3. Code examples
Pass a string that is eventually executed to accept filtering of the interceptor stack
1). Interceptor interface
Package Com.wyl.chain; /** * Interceptor Interface * [email protected] * */ Public Interface Interceptor { /** * Interception method @param str analog string @param Chain Interceptor Stack * /publicvoid doFilter (String str, Interceptorchain chain);}
2). Interceptor implementation Class A
Package Com.wyl.chain; /** * Interceptor A * [email protected] * */ Public class Implements Interceptor { @Override publicvoid doFilter (String str, Interceptorchain chain) { if(Str.indexof ("Hello")!=-1) Chain.executenextinterceptor ( STR); Else Requestutil.addparam (str, "intercepted by a");} }
3). Interceptor Implementation Class B
Package Com.wyl.chain; /** * Interceptor b * [email protected] * */ Public class Implements Interceptor { @Override publicvoid doFilter (String str, Interceptorchain chain) { if(Str.indexof ("World")!=-1) Chain.executenextinterceptor ( STR); Else Requestutil.addparam (str, "intercepted by B");} }
4). Interceptor Implementation Class C
Package Com.wyl.chain; /** * Interceptor c * [email protected] * */ Public class Implements Interceptor { @Override publicvoid doFilter (String str, Interceptorchain chain) { if(Str.indexof ("This")!=-1) chain.executenextinterceptor (str ); Else Requestutil.addparam (str, "blocked by C");} }
5). Interceptor Stack
PackageCom.wyl.chain;Importjava.util.ArrayList;Importjava.util.List;/*** Interceptor Stack * [email protected] * December 27, 2017*/ Public classInterceptorchain {//Interceptor Collection Public StaticList<interceptor> chains=NewArraylist<>(); //Interceptor Index Private Static intIndex=0; //adding interceptors PublicInterceptorchain Addinterceptor (list<interceptor>interceptors) { for(Interceptor interceptor:interceptors) {Chains.add (Interceptor); } return This; } //Execute Next Interceptor Public voidexecutenextinterceptor (String str) {if(Index>chains.size ())return; Chains.get (Index+ +). DoFilter (str, This); } }
6). Parameter class
Package Com.wyl.chain; /** * Key in map, denoted by enumeration * [email protected] * */ Public enum Param { Str,desc}
7). Tool Class
PackageCom.wyl.chain;ImportJava.util.HashMap;ImportJava.util.Map;/*** Map Storage interception status * [email protected] * December 27, 2017*/ Public classRequestutil { Public StaticMap<string,string> map=NewHashmap<>(); Public Static voidAddParam (String str,string description) {map.put (Param.STR.toString (), str); Map.put (Param.DESC.toString (), description); } }
8). Target Execution class
Package Com.wyl.chain; /** * Action to execute * [email protected] * */ Public class Action { // Execute method public void execute () { System.out.println ("original string:" +RequestUtil.map.get (Param.STR.toString ())); SYSTEM.OUT.PRINTLN ("description:" +RequestUtil.map.get (Param.DESC.toString ()));} }
9). Client class
PackageCom.wyl.chain;Importjava.util.ArrayList;Importjava.util.List;/*** Client [email protected] December 27, 2017*/ Public classClient { Public Static voidMain (string[] args) {//Creating the Interceptor stackInterceptorchain chain =NewInterceptorchain (); //Interceptor Collectionlist<interceptor> list =NewArraylist<>(); List.add (NewInterceptora ()); List.add (NewInterceptorb ()); List.add (NewInterceptorc ()); //adding interceptorsInterceptorchain Chainwithinterceptor =chain.addinterceptor (list); //performing interception OperationsChainwithinterceptor.executenextinterceptor ("Hello World"); //Execute target Method NewAction (). Execute (); }}
10). Results
Original string: Hello World Description: Blocked by c
4. Summary
When to choose?
1). Multiple Objects handling one request
2). In the case of ambiguous receivers, one of multiple objects commits a request
3). Dynamically build a collection of requests for an object
Design pattern of responsibility chain in Java design mode