English 中文(简体)
Intercepting Filter Pattern
  • 时间:2024-11-05

Design Pattern - Intercepting Filter Pattern


Previous Page Next Page  

The intercepting filter design pattern is used when we want to do some pre-processing / post-processing with request or response of the apppcation. Filters are defined and appped on the request before passing the request to actual target apppcation. Filters can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern.

    Filter - Filter which will performs certain task prior or after execution of request by request handler.

    Filter Chain - Filter Chain carries multiple filters and help to execute them in defined order on target.

    Target - Target object is the request handler

    Filter Manager - Filter Manager manages the filters and Filter Chain.

    Cpent - Cpent is the object who sends request to the Target object.

Implementation

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

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

Intercepting Filter Pattern UML Diagram

Step 1

Create Filter interface.

Filter.java

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

Step 2

Create concrete filters.

AuthenticationFilter.java

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

DebugFilter.java

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

Step 3

Create Target

Target.java

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

Step 4

Create Filter Chain

FilterChain.java

import java.util.ArrayList;
import java.util.List;

pubpc class FilterChain {
   private List<Filter> filters = new ArrayList<Filter>();
   private Target target;

   pubpc void addFilter(Filter filter){
      filters.add(filter);
   }

   pubpc void execute(String request){
      for (Filter filter : filters) {
         filter.execute(request);
      }
      target.execute(request);
   }

   pubpc void setTarget(Target target){
      this.target = target;
   }
}

Step 5

Create Filter Manager

FilterManager.java

pubpc class FilterManager {
   FilterChain filterChain;

   pubpc FilterManager(Target target){
      filterChain = new FilterChain();
      filterChain.setTarget(target);
   }
   pubpc void setFilter(Filter filter){
      filterChain.addFilter(filter);
   }

   pubpc void filterRequest(String request){
      filterChain.execute(request);
   }
}

Step 6

Create Cpent

Cpent.java

pubpc class Cpent {
   FilterManager filterManager;

   pubpc void setFilterManager(FilterManager filterManager){
      this.filterManager = filterManager;
   }

   pubpc void sendRequest(String request){
      filterManager.filterRequest(request);
   }
}

Step 7

Use the Cpent to demonstrate Intercepting Filter Design Pattern.

InterceptingFilterDemo.java

pubpc class InterceptingFilterDemo {
   pubpc static void main(String[] args) {
      FilterManager filterManager = new FilterManager(new Target());
      filterManager.setFilter(new AuthenticationFilter());
      filterManager.setFilter(new DebugFilter());

      Cpent cpent = new Cpent();
      cpent.setFilterManager(filterManager);
      cpent.sendRequest("HOME");
   }
}

Step 8

Verify the output.

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