Open In App

Intercepting Filter Pattern

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Design components

  • 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.

Java





Output:

Authenticating : Downloads
Log: Downloads
Executing : Downloads

Advantages :

  • Improved reusability: Common code is centralized in pluggable components enhancing reuse.
  • Increased flexibility: Generic common components can be applied and removed declaratively, improving flexibility.

Disadvantages :

  • Information sharing is inefficient in intercepting pattern.

If you like GeeksforGeeks and would like to contribute, you can also write an article using

write.geeksforgeeks.org

or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads