举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > java 过滤器模式 Java 过滤器/标准模式

java 过滤器模式 Java 过滤器/标准模式

2023-03-08 06:18 Java教程

java 过滤器模式 Java 过滤器/标准模式

java 过滤器模式

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

    }  
} 

Java 过滤器/标准模式

Java设计模式 - 过滤器/标准模式


过滤器模式使用不同的条件过滤对象。

这些标准可以通过逻辑操作链接在一起。

过滤器模式是一种结构型模式。

例子

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 过滤器/标准模式全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部