管道过滤器(Pipe-And-Filter)模式
生活随笔
收集整理的這篇文章主要介紹了
管道过滤器(Pipe-And-Filter)模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按照《POSA(面向模式的軟件架構)》里的說法,管道過濾器(Pipe-And-Filter)應該屬于架構模式,因為它通常決定了一個系統的基本架構。管道過濾器和生產流水線類似,在生產流水線上,原材料在流水線上經一道一道的工序,最后形成某種有用的產品。在管道過濾器中,數據經過一個一個的過濾器,最后得到需要的數據。 如果感覺抽象的話大家可以想想網上購物。在網上購物的時候一般都會用到搜索和過濾功能。多數網店不僅支持一次性過濾,而且還可以支持在過濾到的結果中層層過濾直至到找到你所鐘愛的商品。本文就提出一種簡化的實現。由于考慮到過濾器不僅可能是單一條件的,也可能是多個條件的組合。所以就通過Composite Pattern引入了Composite Filter以支持And、Or以及Not等組合條件過濾。類結構圖如下: 代碼實現如下: 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。
{
????????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。
轉載于:https://blog.51cto.com/bj007/345677
總結
以上是生活随笔為你收集整理的管道过滤器(Pipe-And-Filter)模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rhel5之光盘更新终结篇
- 下一篇: 随便唠叨下 最近的事情