管道过滤器(Pipe-And-Filter)模式(出处:http://bj007.blog.51cto.com/1701577/345677)

本文介绍了管道过滤器(Pipe-And-Filter)架构模式,并通过具体代码实例展示了如何设计过滤器部分,包括价格范围过滤器、类别过滤器及复合过滤器等。

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

 按照《POSA(面向模式的软件架构)》里的说法,管道过滤器(Pipe-And-Filter)应该属于架构模式,因为它通常决定了一个系统的基本架构。管道过滤器和生产流水线类似,在生产流水线上,原材料在流水线上经一道一道的工序,最后形成某种有用的产品。在管道过滤器中,数据经过一个一个的过滤器,最后得到需要的数据。
clip_image001
代码实现如下:
namespace FilterDemo 

         public  enum CATAGORY { Food, Drink, Cloth, Office, Other };

         public  class Goods 
        { 
                 public  int Price {  private set; get; } 
                 public CATAGORY Category {  private set; get; }
                 public Goods(CATAGORY category,  int price) 
                { 
                        Category = category; 
                        Price = price; 
                } 
        }

         public  interface Filter 
        { 
                 bool Match(Goods goods); 
        }

         public  class PriceRangeFilter : Filter 
        { 
                 int min; 
                 int max;
                 public PriceRangeFilter( int min,  int max) 
                { 
                         this.min = min; 
                         this.max = max; 
                }
                 public  bool Match(Goods goods) 
                { 
                         return (goods.Price >= min && goods.Price <= max);
                } 
        }

         public  class CategoryFilter : Filter 
        { 
                CATAGORY category;
                 public CategoryFilter(CATAGORY category) 
                { 
                         this.category = category; 
                }
                 public  bool Match(Goods goods) 
                { 
                         return (goods.Category == category); 
                } 
        }

         public  class CompositeFilter : Filter 
        { 
                 protected ArrayList filters =  new ArrayList();
                 public  void AddFilters(  params Filter[] filters) 
                { 
                         this.filters.AddRange(filters); 
                }
                 public  void AddFilter(Filter filter) 
                { 
                         this.filters.Add(filter); 
                }
                 public  bool Match(Goods goods) 
                { 
                         return  false
                } 
        }

         public  class AddFilter : CompositeFilter 
        { 
                 public  bool Match(Goods goods) 
                { 
                         foreach (Filter filter  in filters) 
                        { 
                                 if (!filter.Match(goods)) 
                                         return  false
                        } 
                         return  true
                } 
        }

         public  class OrFilter : CompositeFilter 
        { 
                 public  bool Match(Goods goods) 
                { 
                         foreach(Filter filter  in filters) 
                        { 
                                 if(filter.Match(goods)) 
                                         return  true
                        } 
                         return  false
                } 
        } 
}

  至此,Pipe-And-Filter模式的Filter部分设计已经完成。剩下的就是设计管道,并安装上述各种Filter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值