简单举例如下:
会员搜索过滤模块,要先按姓名搜索,再按编号过滤部分会员.
例子比较简单,按姓名搜索,过滤会员在.两功能上是独立的.
没有依赖性,不过实际中常用来功能有依赖性解决并行开发的.
例子中简单只有一个数据源.实际中常常是用来处理两个数据源.
第一个是作为输入的,第二个则是作为输出的.

谁有更好的理解和应用?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ModeStudy.Pipes_and_Filters
{
    //可用的流动处理数据接口
    public interface IEnableStreamingProcessData<DataSource>
    {
        //例子简单只有一个数据源.
       //实际中常常会是两个数据源.第一个作为,第二个作为输出.
       //记得Java中的过滤器吗?
       //public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
       //void Process(DataSourceInput dataInput,out DataSourceOutput dataOutput)        
        void Process(ref DataSource dataSource);
    }

    //链条式处理类
    public class ProcessChain<DataSource> where DataSource : new()
    {
        private LinkedList<IEnableStreamingProcessData<DataSource>> _links = new LinkedList<IEnableStreamingProcessData<DataSource>>();

        public void AddLink(LinkedListNode<IEnableStreamingProcessData<DataSource>> link)
        {
            _links.AddLast(link);
        }

        public void Process(ref DataSource dataSource)
        {
            foreach (IEnableStreamingProcessData<DataSource> element in _links)
            {
                element.Process(ref dataSource);
            }
        }
    }

    //搜索会员类
    public class MembersSearch : IEnableStreamingProcessData<DataTable>
    {
        private string _searchName;

        public string SearchName
        {
            get { return _searchName; }
            set { _searchName = value; }
        }

        public MembersSearch()
        {
            this.Reset();
        }

        public void Reset()
        {
            _searchName = string.Empty;
        }

        #region IEnableStreamingProcessData<DataTable> 成员

        public void Process(ref DataTable dataSource)
        {
            dataSource.DefaultView.RowFilter = "name like '%" + this.SearchName+"%'";
            dataSource = dataSource.DefaultView.ToTable();
        }

        #endregion
    }

    //会员过滤类
    public class MembersFilter : IEnableStreamingProcessData<DataTable>
    {
        private int _id;

        public int FilterID
        {
            get { return _id; }
            set { _id = value; }
        }

        public MembersFilter()
        {
            this.Reset();
        }

        public void Reset()
        {
            _id = -1;
        }

        #region IEnableStreamingProcessData<DataTable> 成员

        public void Process(ref DataTable dataSource)
        {
            dataSource.DefaultView.RowFilter = "id <>" + this.FilterID.ToString();
            dataSource = dataSource.DefaultView.ToTable();
        }

        #endregion
    }

    //会员搜索单例工厂
    public static class MemberSearchFactory
    {
        private static MembersSearch memSearch;
        private static MembersFilter memFilter;

        public static IEnableStreamingProcessData<DataTable> MemberSearchInstance(string searchName)
        {
            if (null == memSearch)
                memSearch = new MembersSearch();
            memSearch.Reset();
            memSearch.SearchName = searchName;
            return memSearch;
        }

        public static IEnableStreamingProcessData<DataTable> MemberFilterInstance(int filterID)
        {
            if (null == memFilter)
                memFilter = new MembersFilter();
            memFilter.Reset();
            memFilter.FilterID = filterID;
            return memFilter;
        }
    }

    public class Test
    {
        public void Process()
        {
            //构造一个具有 id, name列的DataTable.,并添加三条数据.
            DataTable table = new DataTable();
            DataColumnCollection columns = table.Columns;
            columns.Add(new DataColumn("id",typeof(int)));
            columns.Add(new DataColumn("name",typeof(string)));
            DataRow row = table.NewRow();
            row["id"]=1;
            row["name"]="tyweber";
            table.Rows.Add(row);
            row = table.NewRow();
            row["id"] = 2;
            row["name"] = "admin";
            table.Rows.Add(row);
            row = table.NewRow();
            row["id"] = 3;
            row["name"] = "tyweber_zouyong";
            table.Rows.Add(row);

            //实例化链条处理类,进行筛选DataTable中的数据.
            ProcessChain<DataTable> chain = new ProcessChain<DataTable>();

            IEnableStreamingProcessData<DataTable> memSearch = MemberSearchFactory.MemberSearchInstance("tyweber");
            IEnableStreamingProcessData<DataTable> memFilter = MemberSearchFactory.MemberFilterInstance(1);

            LinkedListNode<IEnableStreamingProcessData<DataTable>> nodeSearch = new LinkedListNode<IEnableStreamingProcessData<DataTable>>(
                memSearch
                );
            LinkedListNode<IEnableStreamingProcessData<DataTable>> nodeFilter = new LinkedListNode<IEnableStreamingProcessData<DataTable>>(
                memFilter
                );

            chain.AddLink(nodeSearch);
            chain.AddLink(nodeFilter);
           
            chain.Process(ref table);

            foreach (DataRow r in table.Rows)
            {
                Console.WriteLine(r["name"]);
            }
        }
    }
}

posted on 2009-03-30 11:22  雨季  阅读(774)  评论(0编辑  收藏  举报