Java 过滤器模式是一种设计模式,它允许开发人员在不修改现有代码的情况下,对应用程序中的请求或响应进行过滤。它使用一系列过滤器来处理请求或响应,以便在不修改原始代码的情况下实现特定的功能。
Java 过滤器模式有助于将复杂的业务逻辑从应用程序中分离出来,从而使代码变得可重用、可扩展、易于理解和易于测试。此外,它还允许开发人员在不修改原始代码的情况下,对请求或响应进行额外的处理。
Java 过滤器模式通常由三部分组成:Filter 接口、FilterChain 类和 FilterManager 类。Filter 接口是一个标准化的 API ,它允许开发人员创建各种不同的过滤器来处理请求或响应。FilterChain 类是一个 Java 集合,它存储要执行的 Filter 对象。FilterManager 类是一个工厂方法,它用于创建 FilterChain 对象并将 Filter 对象链接在一起。
public interface Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); } public class AuthenticationFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { // Do authentication processing here. // Pass request back down the filter chain. chain.doFilter(request, response); } }
过滤器模式使用不同的条件过滤对象。
这些标准可以通过逻辑操作链接在一起。
过滤器模式是一种结构型模式。
import java.util.List; import java.util.ArrayList; class Employee { private String name; private String gender; private String retireStatus; public Employee(String name, String gender, String r) { this.name = name; this.gender = gender; this.retireStatus = r; } public String getName() { return name; } public String getGender() { return gender; } public String getRetireStatus() { return retireStatus; } @Override public String toString() { return "Employee [name=" + name + ", gender=" + gender + ", retireStatus=" + retireStatus + "]"; } } interface Criteria { public List<Employee> meetCriteria(List<Employee> persons); } class CriteriaMale implements Criteria { @Override public List<Employee> meetCriteria(List<Employee> persons) { List<Employee> malePersons = new ArrayList<Employee>(); for (Employee person : persons) { if (person.getGender().equalsIgnoreCase("MALE")) { malePersons.add(person); } } return malePersons; } } class CriteriaFemale implements Criteria { @Override public List<Employee> meetCriteria(List<Employee> persons) { List<Employee> femalePersons = new ArrayList<Employee>(); for (Employee person : persons) { if (person.getGender().equalsIgnoreCase("FEMALE")) { femalePersons.add(person); } } return femalePersons; } } class CriteriaRetire implements Criteria { @Override public List<Employee> meetCriteria(List<Employee> persons) { List<Employee> singlePersons = new ArrayList<Employee>(); for (Employee person : persons) { if (person.getRetireStatus().equalsIgnoreCase("YES")) { singlePersons.add(person); } } return singlePersons; } } class AndCriteria implements Criteria { private Criteria criteria; private Criteria otherCriteria; public AndCriteria(Criteria criteria, Criteria otherCriteria) { this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Employee> meetCriteria(List<Employee> persons) { List<Employee> firstCriteriaPersons = criteria.meetCriteria(persons); return otherCriteria.meetCriteria(firstCriteriaPersons); } } class OrCriteria implements Criteria { private Criteria criteria; private Criteria otherCriteria; public OrCriteria(Criteria criteria, Criteria otherCriteria) { this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Employee> meetCriteria(List<Employee> persons) { List<Employee> firstCriteriaItems = criteria.meetCriteria(persons); List<Employee> otherCriteriaItems = otherCriteria.meetCriteria(persons); for (Employee person : otherCriteriaItems) { if (!firstCriteriaItems.contains(person)) { firstCriteriaItems.add(person); } } return firstCriteriaItems; } } public class Main { public static void main(String[] args) { List<Employee> persons = new ArrayList<Employee>(); persons.add(new Employee("Tom", "Male", "YES")); persons.add(new Employee("Jack", "Male", "NO")); persons.add(new Employee("Jane", "Female", "NO")); persons.add(new Employee("Diana", "Female", "YES")); persons.add(new Employee("Mike", "Male", "NO")); persons.add(new Employee("Bob", "Male", "YES")); Criteria male = new CriteriaMale(); Criteria female = new CriteriaFemale(); Criteria retire = new CriteriaRetire(); Criteria retireMale = new AndCriteria(retire, male); Criteria retireOrFemale = new OrCriteria(retire, female); System.out.println("Males: "); printPersons(male.meetCriteria(persons)); System.out.println("Females: "); printPersons(female.meetCriteria(persons)); System.out.println("Retire Males: "); printPersons(retireMale.meetCriteria(persons)); System.out.println("Retire Or Females: "); printPersons(retireOrFemale.meetCriteria(persons)); } public static void printPersons(List<Employee> persons) { for (Employee person : persons) { System.out.println(person); } } }
上面的代码生成以下结果。
Java设计模式 - 战略模式在策略模式中,可以在运行时更改算法。策略模式是一种行为模式。在策略模式中,我们创建表示各种算法的...
Java面向对象设计 -Java构造函数构造函数是用于在对象创建后立即初始化对象的代码块。构造函数的结构看起来类似于一个方法。声明...
Java面向对象设计 -Java注释反射程序元素上的注释是Java对象。允许您访问其注释的程序元素实现java.lang.reflect.AnnotatedEleme...