文章出處

  迭代器模式(Iterator Pattern),提供一種方法順序訪問一個聚合對象中元素,而不暴露該集合對象的內部表示。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PatternProject
{
    public class People
    {
        public string name
        { get; set; }

        public string sex
        { get; set; }

        public int age
        { get; set; }

        public DateTime JoinDateTime
        { get; set; }

        public People(string name, string sex, int age)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.JoinDateTime = DateTime.Now;
        }
    }

    public interface ISearcher  
    {
        bool Next();  //在迭代時,判斷是否能獲取下一個,外部遍歷必需實現該方法
    }

    public interface IProvince  
    {
        ISearcher GetSearcher();  //獲取迭代器對象
    }


    public class Province : IProvince
    {
        List<People> peopleList = new List<People>();

        public string name
        { get; set; }

        public Province(string name)
        {
            this.name = name;
        }

        public ISearcher GetSearcher()
        {
            return new Searcher(this, "黃某", "男", 45);
        }

        public People this[int index]
        {
            get { return peopleList[index]; }
            //set { peopleList.Add(value); }
        }

        public void Join(People people)
        {
            peopleList.Add(people);
        }

        public int GetPeopleCount()
        {
            return peopleList.Count;
        }
    }


    public class Searcher : People, ISearcher
    {
        Province _province;
        int _currentIndex;

        public Searcher(Province province, string name, string sex, int age)
            : base(name, sex, age)
        {
            _province = province;
            _currentIndex = -1;
        }

        public People Current
        {
            get { return _province[_currentIndex]; }
        }

        public bool Next()
        {
           _currentIndex++;
           return _province.GetPeopleCount() > _currentIndex;
        }

    }


}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PatternProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Province SZProvince = new Province("深圳市");
            SZProvince.Join(new People("張三", "男", 25));
            SZProvince.Join( new People("李四", "男", 35));
            SZProvince.Join(new People("王五", "男", 30));
            SZProvince.Join(new People("趙六", "男", 28));
            SZProvince.Join(new People("謝七", "女", 20));

            Console.WriteLine("{0}共計人口:{1}人", SZProvince.name, SZProvince.GetPeopleCount());
            Searcher srh= (Searcher)SZProvince.GetSearcher();

            Console.WriteLine("人口檢查官:{0}人口普查結果如下:", srh.name);

            while (srh.Next())
            {
                People p=srh.Current;
                Console.WriteLine("{0},{1},{2}歲于{3}入住;", p.name, p.sex, p.age, p.JoinDateTime);
            }

            Console.ReadKey();
        }
    }
}

  


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()