Intercepting Filter Pattern is used to do some pre-processing/post-processing on the application's request or response. Define the filter and apply it on the request before passing it to the actual target application. The filter can do authentication/authorization/logging, or track the request, and then pass the request to the appropriate handler. The following are the entities of this design pattern.
- Filter - The filter performs certain tasks before or after the request handler executes the request.
- Filter Chain - The filter chain has multiple filters and executes them on the Target in the defined order.
- Target - The Target object is the request handler.
- Filter Manager - Filter Manager manages filters and filter chains.
- Client - Client is the object that sends the request to the Target object.
We will create FilterChain, FilterManager, Target, Client as various objects representing entities. AuthenticationFilter and DebugFilter represent entity filters.
InterceptingFilterDemo, our demo class uses Client to demonstrate the intercept filter design pattern.
Create a filter interface Filter.
Create an entity filter.
Create a Target.
Create a filter chain.
Create a filter manager.管理器。
Create a client client.
Use Client to demonstrate the intercept filter design pattern.
Executing the program, outputting results:
Authenticating request: HOME request log: HOME Executing request: HOME