Design Pattern 简明教程

Design Pattern - Intercepting Filter Pattern

当我们希望对请求或应用程序的响应进行一些预处理/后处理时,将使用拦截过滤器设计模式。在将请求传递给实际目标应用程序之前,对请求定义并应用过滤器。过滤器可以对请求进行身份验证/授权/记录或跟踪,然后将请求传递给相应的处理程序。以下是此类设计模式的实体。

The intercepting filter design pattern is used when we want to do some pre-processing / post-processing with request or response of the application. Filters are defined and applied on the request before passing the request to actual target application. 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.

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

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

  3. Target - Target object is the request handler

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

  5. Client - Client is the object who sends request to the Target object.

Implementation

我们将创建 FilterChain、FilterManager、Target 和 Client 作为表示我们实体的不同对象。AuthenticationFilter 和 DebugFilter 表示具体过滤器。

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

我们的演示类 InterceptingFilterDemo 将使用 Client 演示拦截过滤器设计模式。

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

interceptingfilter pattern uml diagram

Step 1

创建过滤器界面。

Create Filter interface.

Filter.java

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

Step 2

创建具体过滤器。

Create concrete filters.

AuthenticationFilter.java

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

DebugFilter.java

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

Step 3

创建目标

Create Target

Target.java

public class Target {
   public 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;

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){
      this.target = target;
   }
}

Step 5

创建过滤器管理程序

Create Filter Manager

FilterManager.java

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 Client

Client.java

public class Client {
   FilterManager filterManager;

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

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

Step 7

使用客户端演示拦截过滤器设计模式。

Use the Client to demonstrate Intercepting Filter Design Pattern.

InterceptingFilterDemo.java

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

验证输出。

Verify the output.

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