Radio

一个小小程序员

0%

过滤器模式

过滤器模式

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

示例演示:
  1. 我们创建一个people实体

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package test.filter;

    import lombok.Data;

    @Data
    public class People {

    private Integer id;
    private String name;

    public People(Integer id, String name) {
    this.id = id;
    this.name = name;
    }
    }
  2. 创建一个peopleId的接口,用于通过id对people分组

    1
    2
    3
    4
    5
    6
    7
    package test.filter;

    import java.util.List;

    public interface PeopleId {
    public List<People> group(List<People> peoples);
    }
  3. 接下来我们按照奇偶对id进行分类,我们创建两个实现类分班对group进行实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package test.filter;

    import java.util.ArrayList;
    import java.util.List;

    public class OddPeople implements PeopleId {
    @Override
    public List<People> group(List<People> peoples) {
    List<People> oddPeoples = new ArrayList<>();
    for (People people : peoples) {
    if (people.getId() % 2 != 0) {
    oddPeoples.add(people);
    }
    }
    return oddPeoples;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package test.filter;

    import java.util.ArrayList;
    import java.util.List;

    public class EvenPeople implements PeopleId {
    @Override
    public List<People> group(List<People> peoples) {
    List<People> evenPeoples = new ArrayList<>();
    for (People people : peoples) {
    if (people.getId() % 2 == 0) {
    evenPeoples.add(people);
    }
    }
    return evenPeoples;
    }
    }
  4. 完成上述步骤,我们就可以开始演示了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package test.filter;

    import java.util.ArrayList;
    import java.util.List;

    public class FilterDemo {
    public static void main(String[] args) {
    List<People> peoples = new ArrayList<>();

    peoples.add(new People(1,"1"));
    peoples.add(new People(2,"2"));
    peoples.add(new People(3,"3"));
    peoples.add(new People(4,"4"));
    peoples.add(new People(5,"5"));
    peoples.add(new People(6,"6"));
    peoples.add(new People(7,"7"));
    peoples.add(new People(8,"8"));
    peoples.add(new People(9,"9"));
    peoples.add(new People(10,"10"));

    PeopleId oddPeople = new OddPeople();
    System.out.println(oddPeople.group(peoples).toString());

    PeopleId evenPeople = new EvenPeople();
    System.out.println(evenPeople.group(peoples).toString());

    }
    }

    运行结果:

    1
    2
    [People(id=1, name=1), People(id=3, name=3), People(id=5, name=5), People(id=7, name=7), People(id=9, name=9)]
    [People(id=2, name=2), People(id=4, name=4), People(id=6, name=6), People(id=8, name=8), People(id=10, name=10)]
    说明:

    过滤器模式就是我们对一组对象进行过滤,java8提供了里面就提供了很多方法来过滤数据,比如刚才的按照奇偶分类我们就可以直接使用java8提供的方法:

    1
    Map<Integer, List<People>> groupList = peoples.stream().collect(Collectors.groupingBy(c -> c.getId()%2));

欢迎关注我的其它发布渠道