过滤器顾名思义,定义一些过滤规则,将符合要求的内容筛选,就比如过滤不同大小或者不同颜色的水果,需要颜色和大小过滤器,筛选条件独立为对象,可以通过灵活组合形成过滤链条。避免大量使用判断语句。
案例代码:筛选不同颜色的苹果
- 被过滤对象(苹果)
// 苹果对象
class Apple {private String color;private int weight;public Apple(String color, int weight) {this.color = color;this.weight = weight;}public String getColor() { return color; }public int getWeight() { return weight; }
}
- 过滤器接口
// 过滤器接口
interface Filter {List<Apple> filter(List<Apple> apples);
}
- 具体过滤器实现
// 颜色过滤器(筛选红色苹果)
class ColorFilter implements Filter {public List<Apple> filter(List<Apple> apples) {List<Apple> result = new ArrayList<>();for (Apple apple : apples) {if ("red".equalsIgnoreCase(apple.getColor())) {result.add(apple);}}return result;}
}// 重量过滤器(筛选重量大于150g的苹果)
class WeightFilter implements Filter {public List<Apple> filter(List<Apple> apples) {List<Apple> result = new ArrayList<>();for (Apple apple : apples) {if (apple.getWeight() > 150) {result.add(apple);}}return result;}
}
- 组合过滤器(多条件筛选)
// 组合过滤器(同时满足多个条件)
class AndFilter implements Filter {private Filter filter1;private Filter filter2;public AndFilter(Filter filter1, Filter filter2) {this.filter1 = filter1;this.filter2 = filter2;}public List<Apple> filter(List<Apple> apples) {List<Apple> temp = filter1.filter(apples);return filter2.filter(temp);}
}
- 使用过滤器模式
public class FilterDemo {public static void main(String[] args) {List<Apple> apples = Arrays.asList(new Apple("Red", 200),new Apple("Green", 160),new Apple("Red", 140));// 单条件筛选:红色苹果Filter colorFilter = new ColorFilter();List<Apple> redApples = colorFilter.filter(apples);System.out.println("红色苹果数量:" + redApples.size()); // 输出:2// 组合筛选:红色且重量>150gFilter weightFilter = new WeightFilter();Filter andFilter = new AndFilter(colorFilter, weightFilter);List<Apple> result = andFilter.filter(apples);System.out.println("符合条件的苹果数量:" + result.size()); // 输出:1}
}
应用场景案例:用户权限过滤
场景描述
筛选出同时满足以下条件的用户:
- 年龄在18岁以上
- 所在城市为“北京”
- 注册时间在2023年以后
代码实现
// 用户对象
class User {private String name;private int age;private String city;private LocalDate registerDate;public User(String name, int age, String city, LocalDate registerDate) {this.name = name;this.age = age;this.city = city;this.registerDate = registerDate;}// Getter方法省略...
}// 年龄过滤器
class AgeFilter implements Filter {private int minAge;public AgeFilter(int minAge) {this.minAge = minAge;}public List<User> filter(List<User> users) {List<User> result = new ArrayList<>();for (User user : users) {if (user.getAge() >= minAge) {result.add(user);}}return result;}
}// 城市过滤器
class CityFilter implements Filter {private String city;public CityFilter(String city) {this.city = city;}public List<User> filter(List<User> users) {List<User> result = new ArrayList<>();for (User user : users) {if (city.equalsIgnoreCase(user.getCity())) {result.add(user);}}return result;}
}// 注册时间过滤器
class DateFilter implements Filter {private LocalDate startDate;public DateFilter(LocalDate startDate) {this.startDate = startDate;}public List<User> filter(List<User> users) {List<User> result = new ArrayList<>();for (User user : users) {if (user.getRegisterDate().isAfter(startDate)) {result.add(user);}}return result;}
}// 使用示例
public class UserFilterDemo {public static void main(String[] args) {List<User> users = Arrays.asList(new User("张三", 25, "北京", LocalDate.of(2024, 1, 1)),new User("李四", 17, "上海", LocalDate.of(2024, 2, 1)),new User("王五", 30, "北京", LocalDate.of(2022, 5, 1)));// 创建过滤器Filter ageFilter = new AgeFilter(18);Filter cityFilter = new CityFilter("北京");Filter dateFilter = new DateFilter(LocalDate.of(2023, 1, 1));// 组合过滤:年龄>=18 && 城市=北京 && 注册时间>2023Filter combinedFilter = new AndFilter(ageFilter, new AndFilter(cityFilter, dateFilter));List<User> validUsers = combinedFilter.filter(users);System.out.println("有效用户数量:" + validUsers.size()); // 输出:1}
}
过滤器模式应用场景
- 数据筛选:
- 电商商品筛选(价格、品牌、评分)
- 日志过滤(错误级别、时间范围)
- 权限系统:
- 用户角色过滤(管理员、VIP用户)
- 数据权限过滤(部门、区域)
- 数据清洗:
- 去除无效数据(空值、异常值)
- 敏感信息过滤(手机号脱敏)
过滤器模式优势
- 灵活组合条件:通过组合多个过滤器实现复杂逻辑
- 解耦过滤逻辑:每个过滤器独立维护,新增条件无需修改已有代码
- 可复用性高:同一过滤器可用于不同场景(如“北京用户筛选”可用于报表和推送)