Intercepting Filter Pattern
Preprocessing and postprocessing of a request refer to actions taken before and after the core processing of that request. Some of these actions determine whether processing will continue, while others manipulate the incoming or outgoing data stream into a form suitable for further processing.
The classic solution consists of a series of conditional checks, with any failed check aborting the request. Nested if/else statements are a standard strategy, but this solution leads to code fragility and a copy-and-paste style of programming, because the flow of the filtering and the action of the filters is compiled into the application.
The key to solving this problem in a flexible and unobtrusive manner is to have a simple mechanism for adding and removing processing components, in which each component completes a specific filtering action.
UML Diagram Intercepting Filter Pattern
- Filter Manager : The FilterManager manages filter processing. It creates the FilterChain with the appropriate filters, in the correct order, and initiates processing.
- FilterChain : The FilterChain is an ordered collection of independent filters.
- FilterOne, FilterTwo : These are the individual filters that are mapped to a target. The FilterChain coordinates their processing.
- Target : The Target is the resource requested by the client.
Let’s see an example of Intercepting Filter Pattern.
Authenticating : Downloads Log: Downloads Executing : Downloads
- Improved reusability: Common code is centralized in pluggable components enhancing reuse.
- Increased flexibility: Generic common components can be applied and removed declaratively, improving flexibility.
- Information sharing is inefficient in intercepting pattern.
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.