• CSharp: Iterator Pattern


       /// <summary>
        /// A simple file handlng class
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class csFile
        {
    
            /// <summary>
            /// 
            /// </summary>
            private string fileName;
    
            /// <summary>
            /// 
            /// </summary>
            StreamReader ts;
            /// <summary>
            /// 
            /// </summary>
            StreamWriter ws;
            /// <summary>
            /// 
            /// </summary>
            private bool opened, writeOpened;
            /// <summary>
            /// 
            /// </summary>
            public csFile()
            {
                init();
            }
            /// <summary>
            /// 
            /// </summary>
            private void init()
            {
                opened = false;
                writeOpened = false;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="file_name"></param>
            public csFile(string file_name)
            {
                fileName = file_name;
                init();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="file_name"></param>
            /// <returns></returns>
            public bool OpenForRead(string file_name)
            {
                fileName = file_name;
                try
                {
                    ts = new StreamReader(fileName);
                    opened = true;
                }
                catch (FileNotFoundException)
                {
                    return false;
                }
                return true;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool OpenForRead()
            {
                return OpenForRead(fileName);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string readLine()
            {
                return ts.ReadLine();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="s"></param>
            public void writeLine(string s)
            {
                ws.WriteLine(s);
            }
            /// <summary>
            /// 
            /// </summary>
            public void close()
            {
                if (opened)
                    ts.Close();
                if (writeOpened)
                    ws.Close();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool OpenForWrite()
            {
                return OpenForWrite(fileName);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="file_name"></param>
            /// <returns></returns>
            public bool OpenForWrite(string file_name)
            {
                try
                {
                    ws = new StreamWriter(file_name);
                    fileName = file_name;
                    writeOpened = true;
                    return true;
                }
                catch (FileNotFoundException)
                {
                    return false;
                }
            }
        }
    

      

      /// <summary>
        /// String Tokenizer class
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class StringTokenizer
        {
    
            /// <summary>
            /// 
            /// </summary>
            private string data, delimiter;
            /// <summary>
            /// 
            /// </summary>
            private string[] tokens;
            /// <summary>
            /// 
            /// </summary>
            private int index;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dataLine"></param>
            public StringTokenizer(string dataLine)
            {
                init(dataLine, " ");
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dataLine"></param>
            /// <param name="delim"></param>
            private void init(String dataLine, string delim)
            {
                delimiter = delim;
                data = dataLine;
                tokens = data.Split(delimiter.ToCharArray());
                index = 0;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dataLine"></param>
            /// <param name="delim"></param>
            public StringTokenizer(string dataLine, string delim)
            {
                init(dataLine, delim);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool hasMoreElements()
            {
                return (index < (tokens.Length));
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string nextToken()
            {
                return nextElement();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string nextElement()
            {
                string s = tokens[index++];
                while ((s.Length <= 0) && (index < tokens.Length))
                    s = tokens[index++];
                return s;
            }
        }
    
      /// <summary>
        /// Summary description for KidIterator.
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class FilteredIterator : IEnumerator
        {
            private ArrayList kids;
            private int index;
            private string club;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="kidz"></param>
            /// <param name="club"></param>
            public FilteredIterator(ArrayList kidz, string club)
            {
                kids = kidz;
                index = 0;
                this.club = club;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                bool more = index < kids.Count - 1;
                if (more)
                {
                    Kid kd = (Kid)kids[++index];
                    more = index < kids.Count;
                    while (more && !kd.getClub().Equals(club))
                    {
                        kd = (Kid)kids[index++];
                        more = index < kids.Count;
                    }
                }
                return more;
            }
            /// <summary>
            /// 
            /// </summary>
            public object Current
            {
                get
                {
                    return kids[index];
                }
            }
            /// <summary>
            /// 
            /// </summary>
            public void Reset()
            {
                index = 0;
            }
        }
    

      

    	/// <summary>
    	/// Summary description for Kid.
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
    	/// </summary>
    	public class Kid
    	{
    		private string frname, lname, club;
    		private int age;
    		private float time;
    		private Hashtable hash;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="line"></param>
    		public Kid(string line) {
    			hash = new Hashtable ();
    			StringTokenizer tok = new StringTokenizer (line);
    			string lnum = tok.nextToken ();
    			frname = tok.nextToken ();
    			//hash.Add (ParseVar.FRNAME , frname);
    			lname = tok.nextToken ();
    			//hash.Add (ParseVar.LNAME , lname);
    			age = Convert.ToInt32 ( tok.nextToken ());
    			//hash.Add (ParseVar.AGE , age);
    			club = tok.nextToken ();
    			//hash.Add (ParseVar.CLUB , club);
    			time = Convert.ToSingle (tok.nextToken ());
    			//hash.Add (ParseVar.TIME , time);
    		}
    		/// <summary>
    		/// 
    		/// </summary>
    		/// <param name="key"></param>
    		/// <returns></returns>
    		public object getData(int key) {
    			return hash[key];
    		}
    		/// <summary>
    		/// 
    		/// </summary>
    		/// <param name="kd"></param>
    		/// <param name="key"></param>
    		/// <returns></returns>
    		public bool compare(Kid kd, int key) {
    			return compareTo(getData(key) , kd.getData (key));
    		}
    		/// <summary>
    		/// 
    		/// </summary>
    		/// <param name="o1"></param>
    		/// <param name="o2"></param>
    		/// <returns></returns>
    		private bool compareTo(object o1, object o2) {
    			if (o1.GetType().Name.Equals  ("String") )
    				return compare_To((string)o1, (string) o2);
    			else
    				return compare_To((float)o1, (float)o2);
    		}
            /// <summary>
            /// 
            /// </summary>
            /// <param name="s1"></param>
            /// <param name="s2"></param>
            /// <returns></returns>
    		private bool compare_To(String s1, String s2) {
    			return s1.CompareTo (s2) >0;
    		}
            /// <summary>
            /// 
            /// </summary>
            /// <param name="k1"></param>
            /// <param name="k2"></param>
            /// <returns></returns>
    		private bool compare_To(int k1, int k2){ 
    			return k1 > k2;
    		}
            /// <summary>
            /// /
            /// </summary>
            /// <param name="f1"></param>
            /// <param name="f2"></param>
            /// <returns></returns>
    		private bool compare_To(float f1, float f2) {
    			return f1 > f2;
    		}
            /// <summary>
            /// /
            /// </summary>
            /// <returns></returns>
    		public string getFrname() {
    			return frname;
    		}
    
            /// <summary>
            /// /
            /// </summary>
            /// <returns></returns>
    		public string getLname() {
    			return lname;
    		}
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
    		public int getAge() {
    			return age;
    		}
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
    		public string getClub() {
    			return club;
    		}
    	}
    

      

       /// <summary>
        /// Summary description for KidData.
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class KidData : IEnumerator
        {
            private ArrayList kids;
            private int index;
            private Hashtable clubs;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="filename"></param>
            public KidData(string filename)
            {
                kids = new ArrayList();
                clubs = new Hashtable();
                csFile fl = new csFile(filename);
                fl.OpenForRead();
                string line = fl.readLine();
                while (line != null)
                {
                    Kid kd = new Kid(line);
                    string club = kd.getClub();
                    if (!clubs.Contains(club))
                    {
                        clubs.Add(club, club);
                    }
                    kids.Add(kd);
                    line = fl.readLine();
                }
                fl.close();
                index = 0;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public IDictionaryEnumerator getClubs()
            {
                return clubs.GetEnumerator();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                index++;
                return index < kids.Count;
            }
            /// <summary>
            /// 
            /// </summary>
            public object Current
            {
                get
                {
                    return kids[index];
                }
            }
            /// <summary>
            /// 
            /// </summary>
            public void Reset()
            {
                index = 0;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public Kid[] getData()
            {
                Kid[] kds = new Kid[kids.Count];
                for (int i = 0; i < kids.Count; i++)
                {
                    kds[i] = (Kid)kids[i];
                }
                return kds;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public int size()
            {
                return kids.Count;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="i"></param>
            /// <returns></returns>
            public Kid getKid(int i)
            {
                if (i >= 0 && i < kids.Count)
                    return (Kid)kids[i];
                else
                    return null;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public KidIterator getIterator()
            {
                return new KidIterator(kids);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="club"></param>
            /// <returns></returns>
            public FilteredIterator getFilteredIterator(string club)
            {
                return new FilteredIterator(kids, club);
            }
    
        }
    

      

       /// <summary>
        /// Summary description for KidIterator.
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class KidIterator : IEnumerator
        {
            private ArrayList kids;
            private int index;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="kidz"></param>
            public KidIterator(ArrayList kidz)
            {
                kids = kidz;
                index = 0;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                index++;
                return index < kids.Count;
            }
            /// <summary>
            /// 
            /// </summary>
            public object Current
            {
                get
                {
                    return kids[index];
                }
            }
            /// <summary>
            /// 
            /// </summary>
            public void Reset()
            {
                index = 0;
            }
        }
    

      

    调用:、

     /// <summary>
        /// Iterator Patterns 迭代器模式
        /// 20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public partial class IteratorPatternsForm : Form
        {
            /// <summary>
            /// 
            /// </summary>
            private KidData kdata;
            /// <summary>
            /// 
            /// </summary>
            private void init()
            {
                kdata = new KidData("50free.txt");
                KidIterator kiter = kdata.getIterator();
                while (kiter.MoveNext())
                {
                    Kid kd = (Kid)kiter.Current;
                    lsKids.Items.Add(kd.getFrname() + " " + kd.getLname());
                }
                IDictionaryEnumerator clubiter = kdata.getClubs();
                while (clubiter.MoveNext())
                {
                    cbClubs.Items.Add((string)clubiter.Value);
                }
    
            }
    
            /// <summary>
            ///
            /// </summary>
            public IteratorPatternsForm()
            {
                InitializeComponent();
                init();
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void IteratorPatternsForm_Load(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void lsClubKids_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cbClubs_SelectedIndexChanged(object sender, EventArgs e)
            {   
                
                string club = (String)cbClubs.SelectedItem;
                FilteredIterator iter = kdata.getFilteredIterator(club);
                lsClubKids.Items.Clear();
                while (iter.MoveNext())
                {
                    Kid kd = (Kid)iter.Current;
                    lsClubKids.Items.Add(kd.getFrname() + " " + kd.getLname());
    
                }
    
            }
          
    

      

    输出:

      

  • 相关阅读:
    封装( 增删改 查 )类
    php注释规范
    php访问mysql数据库
    php 文件限速下载代码
    jQuery鼠标事件汇总
    权限管理
    文件管理 打开-返回上级
    文件操作
    简单的文件上传
    ajax XML
  • 原文地址:https://www.cnblogs.com/geovindu/p/16743387.html
Copyright © 2020-2023  润新知