迭代器模式

C#里面的

 foreach (Component c in child)
            {
                c.Depth(depth + 2);
            }

代码实例一

迭代器抽象类

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

namespace Iterator
{
    abstract class Iterator
    {
        public abstract Object First();
        public abstract Object Next();
        public abstract bool IsDone();
        public abstract Object CurrentItem();
    }
}

聚集抽象类

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

namespace Iterator
{
    abstract class Aggregate
    {
        public abstract Iterator CreateIterator();
    }
}

具体迭代器类 

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

namespace Iterator
{
    class ConcreteIterator : Iterator
    {
        private ConcreteAggregate concreteAggregate;
        private int current = 0;
        public ConcreteIterator(ConcreteAggregate concreteAggregate)
        {
            this.concreteAggregate = concreteAggregate;
        }

        public override object CurrentItem()
        {
            return concreteAggregate[current];
        }

        public override object First()
        {
            return concreteAggregate[0];
        }

        public override bool IsDone()
        {
            return current >= concreteAggregate.Count ? true : false;
        }

        public override object Next()
        {
            Object ret = null;
            current++;
            if(current<concreteAggregate.Count)
            {
                ret = concreteAggregate[current];
            }
            return ret;
        }
    }
}

从后往前遍历

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

namespace Iterator
{
    class ConcreteIteratorDesc : Iterator
    {

        private ConcreteAggregate concreteAggregate;
        private int current = 0;

        public ConcreteIteratorDesc(ConcreteAggregate concreteAggregate)
        {
            this.concreteAggregate = concreteAggregate;
            current = concreteAggregate.Count - 1;
        }

        public override object CurrentItem()
        {
            return concreteAggregate[current];
        }

        public override object First()
        {
            return concreteAggregate[concreteAggregate.Count - 1];
        }

        public override bool IsDone()
        {
            return current < 0 ? true : false;
        }

        public override object Next()
        {
            object ret = null;
            current--;
            if(current>=0)
            {
                ret = concreteAggregate[current];
            }
            return ret;
        }
    }
}

具体聚集类

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

namespace Iterator
{
    class ConcreteAggregate : Aggregate
    {
        private List<Object> items = new List<object>();

        public override Iterator CreateIterator()
        {
            return new ConcreteIterator(this);
        }
        public int Count
        {
            get { return items.Count; }
        }
        public Object this[int index]
        {
            get { return items[index]; }
            set { items.Insert(index, value); }
        }
    }
}

测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Iterator
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            ConcreteAggregate a = new ConcreteAggregate();
            a[0] = "1";
            a[1] = "2";
            a[2] = "3";
            Iterator i = new ConcreteIterator(a);
            Object item = i.First();
            while(!i.IsDone())
            {
                Console.WriteLine(i.CurrentItem());
                i.Next();
            }
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值