IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

三十五、拦截过滤器模式 ( Intercepting Filter )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=2721

拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理

定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上

过滤器可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序

拦截过滤器模式涉及以下实体:

  • 过滤器(Filter) - 过滤器在请求处理程序执行请求之前或之后,执行某些任务。
  • 过滤器链(Filter Chain) - 过滤器链带有多个过滤器,并在 Target 上按照定义的顺序执行这些过滤器。
  • Target - Target 对象是请求处理程序。
  • 过滤管理器(Filter Manager) - 过滤管理器管理过滤器和过滤器链。
  • 客户端(Client) - Client 是向 Target 对象发送请求的对象。

实现

img_1.png

1、 定义类 FilterChainFilterManagerTargetClient 作为表示实体的各种对象
2、 定义类 AuthenticationFilterDebugFilter 表示实体过滤器
3、 定义类 InterceptingFilterDemo 使用 Client 来演示拦截过滤器设计模式

范例

1. 创建过滤器接口 Filter

Filter.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

2. 创建实体过滤器

AuthenticationFilter.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

DebugFilter.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

3. 创建 Target

Target.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

4. 创建过滤器链

FilterChain.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

5. 创建过滤管理器

FilterManager.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

6. 创建客户端 Client

Client.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


public class Client {
   FilterManager filterManager;

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

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

7. 使用 Client 来演示拦截过滤器设计模式

FrontControllerPatternDemo.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/souyunku/tech/gof/InterceptingFilterDemo.java
$ java  com.souyunku.tech.gof.InterceptingFilterDemo
Authenticating request: HOME
request log: HOME
Executing request: HOME

干货推荐

本站推荐:精选优质专栏

附录:设计模式:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(63) 打赏



未经允许不得转载:搜云库技术团队 » 三十五、拦截过滤器模式 ( Intercepting Filter )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367