zoukankan      html  css  js  c++  java
  • 设计模式之迭代器模式

    说起迭代器,大家一定不陌生,经常使用的foreach in 这种循环就是,C#语言已经内置化了迭代器模式,主要是支持对非泛型集合的简单迭代接口IEumerator和公开枚举数IEnumerable。虽然内置了,但是这种模式也有我们学习的必要性。

    代码如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 设计模式之迭代器模式
    {
        public abstract class Iterator//抽象迭代器
        {
            public abstract object First();
            public abstract object Next();
            public abstract object Current();
            public abstract bool IsMax();
        }
        public abstract class Aggregate//抽象被迭代的对象
        {
            public abstract Iterator CreateIterator();
        }
        public class AIterator : Iterator//具体的迭代器
        {
            public AAggregate listA = new AAggregate();//存放被迭代的对象
            private int current=0;
            public AIterator(AAggregate a)//初始化时绑定被迭代的对象
            {
                listA = a;
            }
            public override object First()//获得第一个迭代的对象
            {
                return listA[0];
            }
            public override object Next()//获得当前对象的下一个对象
            {
                current++;
                if (current < listA.Count())
                {
                    return listA[current];
                }
                else
                {
                    return null;
                }
            }
            public override object Current()//获得当前对象
            {
                return listA[current];
            }
            public override bool IsMax()
            {
                return current == listA.Count() ? true : false;
            }
        }
        public class AAggregate : Aggregate//具体的被迭代对象,迭代方向是正向。
        {
            public IList<object> lists = new List<object>();
            public override Iterator CreateIterator()//创被迭代对象的造迭代器
            {
                return new AIterator(this);
            }
            public int Count()//获得被迭代对象的个数
            {
                return lists.Count;
            }
            public object this[int index]//索引器
            {
                get
                {
                    return lists[index];
                }
                set
                {
                    lists.Insert(index, value);
                }
            }
        }
        public class BIterator : Iterator
        {
            public AAggregate blist = new AAggregate();
            private int current=0;
            public BIterator(AAggregate a)
            {
                blist = a;
                current = a.Count() - 1;
            }
            public override object Current()
            {
                return blist[current];
            }
            public override object First()
            {
                return blist[blist.Count()-1];
            }
            public override bool IsMax()
            {
                return current<0? true : false;
            }
            public override object Next()
            {
                current--;
                if(current>=0)
                {
                    return blist[current];
                }
                return null;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                AAggregate a = new AAggregate();
                a[0] = "你好";
                a[1] = "你真的好吗?";
                a[2] = "你他妈有病啊!老子很好";
                AIterator b = new AIterator(a);
                while(!b.IsMax())
                {
                    Console.WriteLine(b.Current());
                    b.Next();
                }
                Console.WriteLine();
                BIterator c = new BIterator(a);
                while(!c.IsMax())
                {
                    Console.WriteLine(c.Current());
                    c.Next();
                }
                Console.Read();
            }
        }
    }

    运行结果:

  • 相关阅读:
    JavaScript--数组的声明与创建
    JavaScript--Object对象的两种表示方法
    上下文模式
    Ajax详解
    JS面向对象之原型链
    JS面向对象特性和值类型与复合类型
    JS面向对象使用面向对象进行开发
    JS中的递归
    前端协作流程
    JavaScript中内存使用规则--堆和栈
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/5589343.html
Copyright © 2011-2022 走看看