欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计模式系列 - 过滤器模式

程序员文章站 2022-05-17 12:50:11
过滤器模式通过使用不同的过滤标准来筛选数据,解耦了多个数据源的数据筛选操作。 介绍 过滤器模式属于结构型模式,它通过将多个不同的过滤标准结合起来从而达到一个统一的过滤标准,使具体的过滤对上层隔离。 类图描述 由上图可知, ICriteria 定义了过滤接口,然后 CriteriaMale 、 Cri ......

过滤器模式通过使用不同的过滤标准来筛选数据,解耦了多个数据源的数据筛选操作。

介绍

过滤器模式属于结构型模式,它通过将多个不同的过滤标准结合起来从而达到一个统一的过滤标准,使具体的过滤对上层隔离。

类图描述

设计模式系列 - 过滤器模式

由上图可知,icriteria 定义了过滤接口,然后 criteriamalecriteriasinglecriteriafemale 为具体的底层数据过滤,接着定义 andcriteriaorcriteria 为组装数据过滤。最后上层直接调用他们这两个即可。

代码实现

1、定义实体类

public class person
{
    private string name;
    private string gender;
    private string maritalstatus;

    public person(string name, string gender, string maritalstatus)
    {
        this.name = name;
        this.gender = gender;
        this.maritalstatus = maritalstatus;
    }

    public string getname() => name;
    public string getgender() => gender;
    public string getmaritalstatus() => maritalstatus;
}

1、定义过滤接口

public interface icriteria
{
    ienumerable<person> meetcriteria(ienumerable<person> persons);
}

2、实现过滤接口

public class criteriafemale : icriteria
{
    public ienumerable<person> meetcriteria(ienumerable<person> persions)
    {
        list<person> femalepersons = new list<person>();
        femalepersons.addrange(persions.where(p => p.getgender() == "female"));
        return femalepersons;
    }
}

public class criteriamale : icriteria
{
    public ienumerable<person> meetcriteria(ienumerable<person> persions)
    {
        list<person> malepersons = new list<person>();
        malepersons.addrange(persions.where(p => p.getgender() == "male"));
        return malepersons;
    }
}

public class criteriasingle : icriteria
{
    public ienumerable<person> meetcriteria(ienumerable<person> persons)
    {
        list<person> malepersons = new list<person>();
        malepersons.addrange(persons.where(p => p.getmaritalstatus() == "single"));
        return malepersons;
    }
}

3、组装过滤器

public class andcriteria : icriteria
{
    private icriteria criteria;
    private icriteria othercriteria;

    public andcriteria(icriteria criteria,icriteria othercriteria)
    {
        this.criteria = criteria;
        this.othercriteria = othercriteria;
    }
    public ienumerable<person> meetcriteria(ienumerable<person> persions)
    {
        ienumerable<person> firstcriteriapersons = criteria.meetcriteria(persions);
        return othercriteria.meetcriteria(firstcriteriapersons);
    }
}

public class orcriteria : icriteria
{
    private icriteria criteria;
    private icriteria othercriteria;
    public orcriteria(icriteria criteria, icriteria othercriteria)
    {
        this.criteria = criteria;
        this.othercriteria = othercriteria;
    }

    public ienumerable<person> meetcriteria(ienumerable<person> persions)
    {
        list<person> firstcriteriaitems = criteria.meetcriteria(persions).tolist();
        ienumerable<person> othercriteriaitems = othercriteria.meetcriteria(persions);
        foreach (var person in othercriteriaitems)
        {
            if (!firstcriteriaitems.contains(person))
            {
                firstcriteriaitems.add(person);
            }
        }
        return firstcriteriaitems;
    }
}

4、上层调用

class program
{
    static void main(string[] args)
    {
        list<person> persons = new list<person>();

        persons.addrange(new[] {
            new person("robert", "male", "single"),
            new person("john", "male", "married"),
            new person("laura", "female", "married"),
            new person("diana", "female", "single"),
            new person("mike", "male", "single"),
            new person("bobby", "male", "single")
        });

        icriteria male = new criteriamale();
        icriteria female = new criteriafemale();
        icriteria single = new criteriasingle();
        icriteria singlemale = new andcriteria(single, male);
        icriteria singleorfemale = new orcriteria(single, female);

        console.writeline("males:");
        printpersons(male.meetcriteria(persons));

        console.writeline("females:");
        printpersons(female.meetcriteria(persons));

        console.writeline("single males");
        printpersons(singlemale.meetcriteria(persons));

        console.writeline("single or females");
        printpersons(singleorfemale.meetcriteria(persons));

        console.readkey();
    }

    private static void printpersons(ienumerable<person> persons)
    {
        foreach (var person in persons)
        {
            console.writeline($"person [name:{person.getname()},gender:{person.getgender()},marital status:{person.getmaritalstatus()}]");
        }
    }
}

总结

在实际开发过程中,如果遇到多个过滤标准的话,可以尝试通过使用过滤器模式来将多个不同的过滤标准组合起来,使上层调用达到统一,底层筛选被隔离。