Skip to content
Related Articles

Related Articles

Improve Article

Intercepting Filter Pattern

  • Last Updated : 16 Feb, 2018

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 program to illustrate
// Intercepting Filter Pattern
import java.util.ArrayList;
import java.util.List;
  
interface Filter 
{
    public void execute(String request);
}
  
class AuthenticationFilter implements Filter 
{
    public void execute(String request)
    {
        System.out.println("Authenticating : " + request);
    }
}
  
class DebugFilter implements Filter 
{
    public void execute(String request)
    {
        System.out.println("Log: " + request);
    }
}
  
class Target 
{
    public void execute(String request)
    {
        System.out.println("Executing : " + request);
    }
}
  
class FilterChain 
{
    private List<Filter> filters = new ArrayList<Filter>();
    private Target target;
  
    public void addFilter(Filter filter)
    {
        filters.add(filter);
    }
  
    public void execute(String request)
    {
        for (Filter filter : filters) 
        {
            filter.execute(request);
        }
        target.execute(request);
    }
  
    public void setTarget(Target target)
    {
        this.target = target;
    }
}
  
class FilterManager 
{
    FilterChain filterChain;
  
   public FilterManager(Target target)
   {
       filterChain = new FilterChain();
        filterChain.setTarget(target);
   }
   public void setFilter(Filter filter)
   {
       filterChain.addFilter(filter);
   }
  
   public void filterRequest(String request)
   {
       filterChain.execute(request);
   }
}
  
class Client 
{
    FilterManager filterManager;
  
    public void setFilterManager(FilterManager filterManager)
    {
        this.filterManager = filterManager;
          
    }
  
    public void sendRequest(String request)
    {
        filterManager.filterRequest(request);
    }
}
  
class InterceptingFilter
{
    public static void main(String[] args) 
    {
        FilterManager filterManager = new FilterManager(new Target());
        filterManager.setFilter(new AuthenticationFilter());
        filterManager.setFilter(new DebugFilter());
  
        Client client = new Client();
        client.setFilterManager(filterManager);
        client.sendRequest("Downloads");
          
    }
}

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.

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 contribute@geeksforgeeks.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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :