Intercepting Filter Pattern

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.

UML diagram that intercepts filter patterns

Step 1

Create a filter interface Filter.

public interface Filter { public void execute(String request); }

Step 2

Create an entity filter.

public class AuthenticationFilter implements Filter { public void execute(String request){ System.out.println("Authenticating request: " + request); } }

public class DebugFilter implements Filter { public void execute(String request){ System.out.println("request log: " + request); } }

Step 3

Create a Target.

public class Target { public void execute(String request){ System.out.println("Executing request: " + request); } }

Step 4

Create a filter chain.

import java.util.ArrayList; import java.util.List; public 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){ = target; } }

Step 5

Create a filter manager.


public 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); } }

Step 6

Create a client client.

public class Client { FilterManager filterManager; public void setFilterManager(FilterManager filterManager){ this.filterManager = filterManager; } public void sendRequest(String request){ filterManager.filterRequest(request); } }

Step 7

Use Client to demonstrate the intercept filter design pattern.

public class InterceptingFilterDemo { 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("HOME"); } }

Step 8

Executing the program, outputting results:

Authenticating request: HOME
request log: HOME
Executing request: HOME

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition