Skip to main content
 首页 » 编程设计

Java 拦截过滤器模式

2022年07月19日25虾米姐

1.拦截过滤器模式简介

拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理。定义过滤器,并在把请求传给实际目
标应用程序之前应用在请求上。过滤器可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序。

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


2.示例Demo

import java.util.List; 
import java.util.ArrayList; 
 
interface Filter { 
    void execute(String request); 
} 
 
class CheckFilter implements Filter { 
    public void execute(String request) { 
        System.out.println("Check filter pass, request=" + request); 
    } 
} 
 
class ResourceFilter implements Filter { 
    public void execute(String request) { 
        System.out.println("Resource filter pass, request=" + request); 
    } 
} 
 
class AuthenticFilter implements Filter { 
    public void execute(String request) { 
        System.out.println("Authentic filter pass, request=" + request); 
    } 
} 
 
 
class Target { 
    public void executeTarget(String request) { 
        System.out.println("Execute Target, request=" + request); 
    } 
} 
 
 
class FilterChain { 
    private List<Filter> filters = new ArrayList<Filter>(); 
    private Target target; 
 
    public void addFilter(Filter filter) { 
        filters.add(filter); 
    } 
 
    public void setTarget(Target target) { 
        this.target = target; 
    } 
 
    public void execute(String request) { 
        for (Filter filter : filters) { 
            filter.execute(request); 
        } 
        target.executeTarget(request); 
    } 
} 
 
class FilterManager { 
    private FilterChain filterChain; 
    public FilterManager(Target target) { 
        filterChain = new FilterChain(); 
        filterChain.setTarget(target); 
    } 
    public void setFilter(Filter filter) { 
        filterChain.addFilter(filter); 
    } 
    public void execute(String request) { 
        filterChain.execute(request); 
    } 
} 
 
class Client { 
    FilterManager filterManager; 
    public void setFilterManager(FilterManager filterManager) { 
        this.filterManager = filterManager; 
    } 
 
    public void sendFilterRequest(String request) { 
        filterManager.execute(request); 
    } 
} 
 
 
 
class InterceptingFilterDemo { 
    public static void main(String args[]) { 
        FilterManager filterManager = new FilterManager(new Target()); 
        filterManager.setFilter(new CheckFilter()); 
        filterManager.setFilter(new ResourceFilter()); 
        filterManager.setFilter(new AuthenticFilter()); 
 
        Client client = new Client(); 
        client.setFilterManager(filterManager); 
        client.sendFilterRequest("firewall"); 
    } 
} 
 
 
/* 
$ java InterceptingFilterDemo  
Check filter pass, request=firewall 
Resource filter pass, request=firewall 
Authentic filter pass, request=firewall 
Execute Target, request=firewall 
 
*/

参考:http://www.runoob.com/design-pattern/intercepting-filter-pattern.html


本文参考链接:https://www.cnblogs.com/hellokitty2/p/10747542.html