过滤器模式(Filter Pattern)

title

过滤器模式(Filter Pattern),又称为标准模式(Criteria Pattern),是一种结构型设计模式,它可以通过一系列条件对数据进行筛选和过滤,从而实现数据的筛选和处理。

在过滤器模式中,有三个主要角色:

  • 过滤器接口(Filter Interface):定义了过滤器的基本操作,包括对数据进行筛选和过滤的方法;
  • 具体过滤器(Concrete Filter):实现了过滤器接口,具体实现了对数据的筛选和过滤逻辑;
  • 过滤器链(Filter Chain):将多个过滤器组合成一个过滤器链,通过一系列过滤器对数据进行筛选和处理。

以下是一个简单的 Java 实现示例,实现对商品列表的筛选和过滤:

首先,定义商品类:

public class Product {
    private String name;
    private String category;
    private double price;
    // 省略 getter 和 setter 方法
}

接着,定义过滤器接口 Filter:

public interface Filter {
    public List<Product> filter(List<Product> products);
}

然后,定义具体过滤器,比如按照商品类别过滤的过滤器:

public class CategoryFilter implements Filter {
    private String category;
    public CategoryFilter(String category) {
        this.category = category;
    }
    public List<Product> filter(List<Product> products) {
        List<Product> result = new ArrayList<Product>();
        for (Product p : products) {
            if (p.getCategory().equals(category)) {
                result.add(p);
            }
        }
        return result;
    }
}

再定义按照价格区间过滤的过滤器:

public class PriceRangeFilter implements Filter {
    private double minPrice;
    private double maxPrice;
    public PriceRangeFilter(double minPrice, double maxPrice) {
        this.minPrice = minPrice;
        this.maxPrice = maxPrice;
    }
    public List<Product> filter(List<Product> products) {
        List<Product> result = new ArrayList<Product>();
        for (Product p : products) {
            if (p.getPrice() >= minPrice && p.getPrice() <= maxPrice) {
                result.add(p);
            }
        }
        return result;
    }
}

最后,定义过滤器链 FilterChain:

public class FilterChain implements Filter {
    private List<Filter> filters;
    public FilterChain() {
        this.filters = new ArrayList<Filter>();
    }
    public void addFilter(Filter filter) {
        filters.add(filter);
    }
    public List<Product> filter(List<Product> products) {
        List<Product> result = products;
        for (Filter filter : filters) {
            result = filter.filter(result);
        }
        return result;
    }
}

现在我们可以在客户端代码中使用过滤器模式来对商品列表进行筛选和过滤:

public class Client {
    public static void main(String[] args) {
        List<Product> products = new ArrayList<Product>();
        products.add(new Product("iPhone", "Electronics", 800.0));
        products.add(new Product("iPad", "Electronics", 1200.0));
        products.add(new Product("T-shirt", "Clothing", 30.0));
        products.add(new Product("Jeans", "Clothing", 50.0));

        // 构建过滤器链
        FilterChain filterChain = new FilterChain();
        filterChain.addFilter(new CategoryFilter("Electronics"));
        filterChain.addFilter(new PriceRangeFilter(0.0, 1000.0));

        // 筛选和过滤商品列表
        List<Product> filteredProducts = filterChain.filter(products);

        // 输出结果
        for (Product p : filteredProducts) {
            System.out.println(p.getName() + " " + p.getCategory() + " " + p.getPrice());
        }
    }
}

这个例子中,我们先构建了一个商品列表,然后通过构建过滤器链来筛选和过滤商品列表。在这个过程中,我们使用了两个具体的过滤器,一个按照商品类别过滤的过滤器,一个按照价格区间过滤的过滤器。我们通过将这两个过滤器添加到过滤器链中来构建一个复合过滤器,从而实现了对商品列表的复合筛选和过滤。

最后,我们将过滤后的商品列表输出到控制台。

输出结果如下:

iPhone Electronics 800.0
T-shirt Clothing 30.0
Jeans Clothing 50.0

可以看到,按照过滤器链中的配置,我们筛选出了价格低于 1000 元且商品类别为 Electronics 的商品,最终输出了 iPhone 这一项。同时,Clothing 类别下的 T-shirt 和 Jeans 商品也被保留了下来。

powered by Gitbook© 2023 编外计划 | 最后修改: 2023-11-24 03:37:01

results matching ""

    No results matching ""