| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 718 人关注过本帖
标题:模式设计之迭代器
只看楼主 加入收藏
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
模式设计之迭代器
using System;
using System.Collections.Generic;
using System.Text;
namespace _0090901
{
    abstract class Iterator
    {
        public abstract object First();
        public abstract object Next();
        public abstract bool  IsDone();
        public abstract object CurrentItem();
    }
    abstract class Aggregate
    {
        public abstract Iterator CreatIterator();
    }
    class ConcreateIterator : Iterator
    {
        private ConcreateAggregate aggregate;
        private int current = 0;
        public ConcreateIterator(ConcreateAggregate aggregate)
        {
            this.aggregate = aggregate;
         
        }
        public override object First()
        {
            return aggregate[0];
        }
        public override object Next()
        {
            object ret = null;
            current++;
            if (current <aggregate.Count)
            {
                ret = aggregate[current];
            }
            return ret;
        }
        public override bool IsDone()
        {
            return current >= aggregate.Count ? true : false;
        }
        public override object CurrentItem()
        {
            return aggregate[current];  
        }
    };
    class ConcreateAggregate : Aggregate
    {
        private IList<object> items = new List<object>();
        public override Iterator CreatIterator()
        {
            return new ConcreateIterator(this);
        }
        public int Count
        {
            get { return items.Count; }
        }
        public object this[int index]
        {
            get { return items[index]; }
            set { items.Insert(index, value); }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ConcreateAggregate a = new ConcreateAggregate();
            a[0] = "大鸟";
            a[1] = "小菜";
            a[2] = "行李";
            a[3] = "老外";
            a[4] = "公交内部员工";
            a[5] = "小偷";
            Iterator i = new ConcreateIterator(a);
            object item = i.First();
            while (!i.IsDone())
            {
                Console.WriteLine("{0}请买票!", i.CurrentItem());
                i.Next();
            }
            Console.Read();
        }
    }
}
23个模式很经典,最近一直在自己写着玩玩,没事贴点,怀恋我还是在写代码的。。哎。。

[ 本帖最后由 sunkaidong 于 2009-9-2 19:40 编辑 ]
搜索更多相关主题的帖子: 设计 模式 
2009-09-02 19:39
快速回复:模式设计之迭代器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043158 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved