Intercepting Filter Pattern

We are going to create a FilterChain,FilterManager, Target, Client as various objects representing our entities.AuthenticationFilter and DebugFilter represent concrete filters.

InterceptingFilterDemo, our demo class, will use Client to demonstrate Intercepting Filter Design Pattern.

Intercepting Filter Pattern UML Diagram

Step 1

Create Filter interface.

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

Step 2

Create concrete filters.

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 Target

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

Step 4

Create 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){

   public void execute(String request){
      for (Filter filter : filters) {

   public void setTarget(Target target){ = target;

Step 5

Create Filter Manager

public class FilterManager {
   FilterChain filterChain;

   public FilterManager(Target target){
      filterChain = new FilterChain();
   public void setFilter(Filter filter){

   public void filterRequest(String request){

Step 6

Create Client

public class Client {
   FilterManager filterManager;

   public void setFilterManager(FilterManager filterManager){
      this.filterManager = filterManager;

   public void sendRequest(String request){

Step 7

Use the Client to demonstrate Intercepting 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();

Step 8

Verify the output.

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