设计模式笔记 – 拦截过滤器模式

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

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

示例

我们将创建 FilterChainFilterManagerTargetClient 作为表示实体的各种对象。AuthenticationFilterDebugFilter 表示实体过滤器。

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

Java 实现

创建过滤器接口 Filter

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

创建实体过滤器

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

创建 Target

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

创建过滤器链

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) {
        filter.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;
    }
}

创建过滤管理器

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

创建客户端 Client

public class Client {
    FilterManager filterManager;
    public void setFilterManager(FilterManager filterManager) {
        this.filterManager = filterManager;
    }
    public void sendRequest(String request) {
        filterManager.filterRequest(request);
    }
}

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

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

        Cilent client = new Client();
        client.setFilterManager(filterManager);
        client.sendRequest("HOME");
    }
}

Python 实现

import abc

class Filter(abc.ABC):
    @abc.abstractmethod
    def execute(self, request: str): ...

class AuthenticationFilter(Filter):
    def execute(self, request: str):
        print(f"Authenticating request: {request}")

class DebugFilter(Filter):
    def execute(self, request: str):
        print(f"request log: {request}")

class Target:
    def execute(self, request: str):
        print(f"Executing request: {request}")

class FilterChain:
    def __init__(self):
        self.filters = []
        self.target = None

    def add_filter(self, filter: Filter):
        self.filters.append(filter)

    def execute(self, request: str):
        for filter in self.filters:
            filter.execute(request)
        self.target.execute(request)

    def set_target(self, target: Target):
        self.target = target

class FilterManager:
    def __init__(self, target: Target):
        self.filter_chain = FilterChain()
        self.filter_chain.set_target(target)

    def set_filter(self, filter: Filter):
        self.filter_chain.add_filter(filter)

    def filter_request(self, request: str):
        self.filter_chain.execute(request)

class Client:
    def __init__(self):
        self.filter_manager = None

    def set_filter_manager(self, filter_manager: FilterManager):
        self.filter_manager = filter_manager

    def send_request(self, request: str):
        self.filter_manager.filter_request(request)

if __name__ == "__main__":
    filter_manager = FilterManager(Target())
    filter_manager.set_filter(AuthenticationFilter())
    filter_manager.set_filter(DebugFilter())

    client = Client()
    client.set_filter_manager(filter_manager)
    client.send_request("HOME")

You may also like...

发表回复

您的电子邮箱地址不会被公开。