设计模式——过滤模式

过滤模式用于在创建对象前,根据业务需求组合过滤器对对象属性进行验证。常见场景如对象插入数据库前的字段验证。通过不同过滤器的组合,实现插拔式过滤。本文以Person对象为例,通过体重、身高、年龄的过滤条件区分儿童和年轻人,展示了具体接口和过滤类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

过滤模式是开发人员根据业务需求对过滤器进行组合也就是多角度过滤对象,来达到获取聚合的目的。
使用场景:
创建对象的时候需要将对象插入到数据库之前对对象中的字段进行验证,有的对象需要验证一个字段有的需要验证多个字段,这种情况我们可以通过对过滤器进行组合来实现对某一个对象中的一个或者多个属性达到验证的目的。
可以根据对象对不同对过滤器进行组合实现过滤器的插拔式。
代码走起来:
代码所要实现的场景:想要根据person的Weight、Height、Age来区分儿童和年轻人,当然了如果通过Linq中的过滤方法一个Where过滤条件就可以获取到自己想要的结果,但是如果业务很复杂无法通过一个简单的Where就能搞定那么还是要有单独的接口来实现一个一个过滤器,在这里暂且规定:儿童属性 Weight(体重)小于50斤、Height(身高)低于1.2米、Age(年龄)小于9岁;年轻人的规定Weight(体重)大于50斤、Height(身高)高于1.2米、Age(年龄)大于9岁。
具体的代码接口过滤类:

public interface IFilter
    {
        List<Person> Filter(List<Person> personList);
    }

儿童的实现接口分别是ChildrenWeightFilter(体重过滤)、ChildrenHeightFilter(身高过滤)、ChildrenAgeFilter(年龄过滤)

public  class ChildrenWeightFilter:IFilter
    {
        /// <summary>
        /// 体重低于五十斤的人为孩童--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
        {
            return personList.Where(x => x.Weight < 50).ToList();
        }
    }
    public class ChildrenHeightFilter : IFilter
    {
        /// <summary>
        /// 身高小于1.2米的人为孩童--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
        {
            return personList.Where(x => x.Height < 1.2).ToList();
        }
    }
    public  class ChildrenAgeFilter:IFilter
    {

        /// <summary>
        /// 身高年龄低于9岁的人为孩童--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
         {
             return personList.Where(x => x.Age < 9).ToList();
         }
    }

对应的年轻人过滤YoungHeightFilter(身高过滤)、YoungWeightFilter(体重过滤)、YoungAgeFilter(年龄过滤)

public class YoungHeightFilter:IFilter
    {
        /// <summary>
        /// 身高大于1.2米的人为青年--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
        {
            return personList.Where(x => x.Height >1.2).ToList();
        }
    }
public class YoungWeightFilter:IFilter
    {
        /// <summary>
        /// 体重超过五十斤的人为年轻人--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
        {
            return personList.Where(x => x.Weight > 50).ToList();
        }
    }
    public class YoungAgeFilter : IFilter
    {
        /// <summary>
        /// 身高年龄大于9岁的人为青年--假设
        /// </summary>
        /// <param name="personList"></param>
        /// <returns></returns>
        public List<Person> Filter(List<Person> personList)
        {
            return personList.Where(x => x.Age >9).ToList();
        }
    }

添加person类以及运行方法:

public class Person
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 身高
        /// </summary>
        public double Height { get; set; }
        /// <summary>
        /// 体重
        /// </summary>
        public int Weight { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }



        static void Main(string[] arge)
        {
            List<Person> persons = new List<Person>
            {
                new Person() {Weight = 15, Height = 1,Age=8, Name = "我叫Children"},
                new Person() {Weight = 80, Height = 1.8, Age = 20,Name = "我叫Young"}
            };
            //根据自己需要组合不同的过滤条件----此处来组合小孩童的过滤条件---插拔式
            List<IFilter> chilFilters = new List<IFilter>()
            {
                new ChildrenAgeFilter(),
                new ChildrenHeightFilter(),
                new ChildrenWeightFilter()
             };
            var childrenFilterBusiness = new FilterBusiness(chilFilters);
            var childrens = childrenFilterBusiness.FilterPerson(persons);
            childrens.ForEach(x =>
            {
                Console.WriteLine($"我是小宝宝我的名字叫{x.Name}我身高是{x.Height}米 我的体重是{x.Weight}斤");
            });
            //根据自己需要组合不同的过滤条件----此处来组合青年的过滤条件---插拔式
            List<IFilter> youngFilters = new List<IFilter>()
            {
                new YoungAgeFilter(),
                new YoungHeightFilter(),
                new YoungWeightFilter()
             };
            var youngFilterBusiness = new FilterBusiness(youngFilters);
            var youong = youngFilterBusiness.FilterPerson(persons);
            youong.ForEach(x =>
            {
                Console.WriteLine($"我是小青年我的名字叫{x.Name}我身高是{x.Height}米 我的体重是{x.Weight}斤");
            });

            Console.ReadLine();
        }
    }

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值