日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

管道过滤器(Pipe-And-Filter)模式

發(fā)布時間:2025/3/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 管道过滤器(Pipe-And-Filter)模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
按照《POSA(面向模式的軟件架構(gòu))》里的說法,管道過濾器(Pipe-And-Filter)應(yīng)該屬于架構(gòu)模式,因?yàn)樗ǔQ定了一個系統(tǒng)的基本架構(gòu)。管道過濾器和生產(chǎn)流水線類似,在生產(chǎn)流水線上,原材料在流水線上經(jīng)一道一道的工序,最后形成某種有用的產(chǎn)品。在管道過濾器中,數(shù)據(jù)經(jīng)過一個一個的過濾器,最后得到需要的數(shù)據(jù)。 如果感覺抽象的話大家可以想想網(wǎng)上購物。在網(wǎng)上購物的時候一般都會用到搜索和過濾功能。多數(shù)網(wǎng)店不僅支持一次性過濾,而且還可以支持在過濾到的結(jié)果中層層過濾直至到找到你所鐘愛的商品。本文就提出一種簡化的實(shí)現(xiàn)。由于考慮到過濾器不僅可能是單一條件的,也可能是多個條件的組合。所以就通過Composite Pattern引入了Composite Filter以支持And、Or以及Not等組合條件過濾。類結(jié)構(gòu)圖如下: 代碼實(shí)現(xiàn)如下: 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部分設(shè)計已經(jīng)完成。剩下的就是設(shè)計管道,并安裝上述各種Filter。

轉(zhuǎn)載于:https://blog.51cto.com/bj007/345677

總結(jié)

以上是生活随笔為你收集整理的管道过滤器(Pipe-And-Filter)模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。