设计模式之使用Java 流式过滤实现过滤器模式链式过滤

Posted boonya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之使用Java 流式过滤实现过滤器模式链式过滤相关的知识,希望对你有一定的参考价值。

使用流式过滤器处理过滤用户所在的省、市、年龄、性别等。重点是后面的链式过滤器。

目标过滤User

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class User 

    private String id;
    private String name;
    private String provinceCode;
    private String cityCode;
    private int age;
    private int gender;

定义过滤器接口

public interface FilterService<T> 

    /**
     * 过滤器方法
     * @param list
     * @param value
     * @return
     */
    List<T> filter(List<T> list,String value);

定义各种过滤器实现

public class AgeFilter implements FilterService<User> 

    @Override
    public List<User> filter(List<User> list,String value) 
        int age = Integer.parseInt(value);
        return list.stream().filter(user -> user.getAge() >= age).collect(Collectors.toList());
    


public class GenderFilter implements FilterService<User> 

    @Override
    public List<User> filter(List<User> list,String value) 
        int gender = Integer.parseInt(value);
        return list.stream().filter(user -> user.getGender() == gender).collect(Collectors.toList());
    


public class ProvinceFilter implements FilterService<User> 

    @Override
    public List<User> filter(List<User> list,String value) 
        return list.stream().filter(user -> user.getProvinceCode().equalsIgnoreCase(value)).collect(Collectors.toList());
    


public class CityFilter implements FilterService<User> 

    @Override
    public List<User> filter(List<User> list,String value) 
        return list.stream().filter(user -> user.getCityCode().equalsIgnoreCase(value)).collect(Collectors.toList());
    

过滤器单元测试

@Slf4j
public class FilterTests 

    static List<User> userList = new ArrayList<>();

    static 
        userList.add(new User(UUID.randomUUID().toString(),"boonya1","Sichuan","Chengdu",12,1));
        userList.add(new User(UUID.randomUUID().toString(),"boonya2","Sichuan","Leshan",8,0));
        userList.add(new User(UUID.randomUUID().toString(),"boonya3","Sichuan","Chengdu",30,1));
        userList.add(new User(UUID.randomUUID().toString(),"boonya4","Sichuan","Ziyang",58,0));
        userList.add(new User(UUID.randomUUID().toString(),"boonya5","Sichuan","Chengdu",80,1));
    

    @Test
    public void age()
        List<User> users = new AgeFilter().filter(userList,"18");
        log.info("==age==filter==data = ",users);
    

    @Test
    public void province()
        List<User> users = new ProvinceFilter().filter(userList,"Sichuan");
        log.info("==province==filter==data = ",users);
    

    @Test
    public void city()
        List<User> users = new CityFilter().filter(userList,"Chengdu");
        log.info("==city==filter==data = ",users);
    

    @Test
    public void gender()
        List<User> users = new GenderFilter().filter(userList,"1");
        log.info("==city==filter==data = ",users);
    

输出:

16:22:12.036 [main] INFO com.example.test.FilterTests - ==city==filter==data = [User(id=cfa84564-ebcc-4cf1-a52f-e32d88216eae, name=boonya1, provinceCode=Sichuan, cityCode=Chengdu, age=12, gender=1), User(id=adc0edbe-f8dd-43ba-a651-39ade01229a0, name=boonya3, provinceCode=Sichuan, cityCode=Chengdu, age=30, gender=1), User(id=20681388-c215-4933-8522-585212b09994, name=boonya5, provinceCode=Sichuan, cityCode=Chengdu, age=80, gender=1)]
16:22:15.851 [main] INFO com.example.test.FilterTests - ==province==filter==data = [User(id=cfa84564-ebcc-4cf1-a52f-e32d88216eae, name=boonya1, provinceCode=Sichuan, cityCode=Chengdu, age=12, gender=1), User(id=665ea3b8-7323-42fd-bcbd-20e8c808be1a, name=boonya2, provinceCode=Sichuan, cityCode=Leshan, age=8, gender=0), User(id=adc0edbe-f8dd-43ba-a651-39ade01229a0, name=boonya3, provinceCode=Sichuan, cityCode=Chengdu, age=30, gender=1), User(id=b3f6c270-e431-490a-be93-7eee084352e5, name=boonya4, provinceCode=Sichuan, cityCode=Ziyang, age=58, gender=0), User(id=20681388-c215-4933-8522-585212b09994, name=boonya5, provinceCode=Sichuan, cityCode=Chengdu, age=80, gender=1)]
16:22:25.013 [main] INFO com.example.test.FilterTests - ==age==filter==data = [User(id=adc0edbe-f8dd-43ba-a651-39ade01229a0, name=boonya3, provinceCode=Sichuan, cityCode=Chengdu, age=30, gender=1), User(id=b3f6c270-e431-490a-be93-7eee084352e5, name=boonya4, provinceCode=Sichuan, cityCode=Ziyang, age=58, gender=0), User(id=20681388-c215-4933-8522-585212b09994, name=boonya5, provinceCode=Sichuan, cityCode=Chengdu, age=80, gender=1)]
16:22:26.528 [main] INFO com.example.test.FilterTests - ==city==filter==data = [User(id=cfa84564-ebcc-4cf1-a52f-e32d88216eae, name=boonya1, provinceCode=Sichuan, cityCode=Chengdu, age=12, gender=1), User(id=adc0edbe-f8dd-43ba-a651-39ade01229a0, name=boonya3, provinceCode=Sichuan, cityCode=Chengdu, age=30, gender=1), User(id=20681388-c215-4933-8522-585212b09994, name=boonya5, provinceCode=Sichuan, cityCode=Chengdu, age=80, gender=1)]

链式过滤接口

链式过滤,就是将多个过滤器都加进来进行处理。

public interface FilterChainService<T> 

    FilterChainService addChain(FilterService<T> filterService);

    List<T> filter(List<T> dataList, String[] args);

链式过滤器实现

这里还是以用户为例,进行链式过滤。

public class UserChainFilter implements FilterChainService<User> 

    List<FilterService> filterServices = new ArrayList<>();

    @Override
    public FilterChainService addChain(FilterService filterService) 
        filterServices.add(filterService);
        return this;
    


    @Override
    public List<User> filter(List<User> dataList, String[] args) 
        Objects.requireNonNull(args);
        int count = filterServices.size();
        FilterService filterService;
        for (int i = 0; i < count; i++) 
            filterService = filterServices.get(i);
            dataList = filterService.filter(dataList,args[i]);
            if(CollectionUtils.isEmpty(dataList))
                break;
            
        
        return dataList;
    

链式过滤单元测试

@Slf4j
public class UserChainFilterTests 

    static List<User> userList = new ArrayList<>();

    static 
        userList.add(new User(UUID.randomUUID().toString(),"boonya1","Sichuan","Chengdu",12,1));
        userList.add(new User(UUID.randomUUID().toString(),"boonya2","Sichuan","Leshan",8,0));
        userList.add(new User(UUID.randomUUID().toString(),"boonya3","Sichuan","Chengdu",30,1));
        userList.add(new User(UUID.randomUUID().toString(),"boonya4","Sichuan","Ziyang",58,0));
        userList.add(new User(UUID.randomUUID().toString(),"boonya5","Sichuan","Chengdu",80,1));
    

    @Test
    public void chain()
        FilterChainService<User> filterChainService = new UserChainFilter()
                .addChain(new ProvinceFilter())
                .addChain(new AgeFilter())
                .addChain(new GenderFilter());
        List<User> users = filterChainService.filter(userList,new String[]"Sichuan","18","1");
        log.info("===users=--size=-=",users.size(),users);
    

输出:

17:17:36.532 [main] INFO com.example.test.FilterChainTests - ===users=--size=2-=[User(id=5287ae1f-11f1-4106-b355-b258f21c668a, name=boonya3, provinceCode=Sichuan, cityCode=Chengdu, age=30, gender=1), User(id=968849e7-a12b-44a9-9473-deb1cf06daf3, name=boonya5, provinceCode=Sichuan, cityCode=Chengdu, age=80, gender=1)]

以上是关于设计模式之使用Java 流式过滤实现过滤器模式链式过滤的主要内容,如果未能解决你的问题,请参考以下文章

listwidget 实现横向流式排布关键以及过滤注意事项

Java进阶篇设计模式之六 ----- 组合模式和过滤器模式

java 设计模式之过滤器模式(Filter)

63 - 责任链模式之过滤器、拦截器

浅谈Java五大设计原则之责任链模式

java中过滤器模式详解和使用方法