Iterator 提供一种方法顺序访问一个对象中各个元素,而又不需要暴露该对象的内部表示...

ContractedBlock.gif ExpandedBlockStart.gif Aggregate
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Iterator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface Aggregate
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        Iterator CreateIterator();
10ExpandedSubBlockEnd.gif    }

11ExpandedBlockEnd.gif}

12None.gif
ContractedBlock.gif ExpandedBlockStart.gif ConcreteAggregate
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Iterator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ConcreteAggregate : Aggregate
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ContractedSubBlock.gifExpandedSubBlockStart.gif        Aggregate 成员#region Aggregate 成员
10InBlock.gif
11InBlock.gif        public Iterator CreateIterator()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            return new ConcreteIterator(this);
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16ExpandedSubBlockEnd.gif        #endregion

17InBlock.gif
18InBlock.gif        private System.Collections.ArrayList items = new System.Collections.ArrayList();
19InBlock.gif
20InBlock.gif        public int Count
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            get
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                return items.Count;
25ExpandedSubBlockEnd.gif            }

26ExpandedSubBlockEnd.gif        }

27InBlock.gif
28InBlock.gif        public object this[int index]
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            get
31ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
32InBlock.gif                return (object)items[index];
33ExpandedSubBlockEnd.gif            }

34InBlock.gif            set
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36InBlock.gif                items.Insert(index, value);
37ExpandedSubBlockEnd.gif            }

38ExpandedSubBlockEnd.gif        }

39ExpandedSubBlockEnd.gif    }

40ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Iterator
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Iterator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface Iterator
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        object First();
10InBlock.gif        object Next();
11InBlock.gif        bool isDone();
12InBlock.gif        object Current();
13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif ConcreteIterator
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Iterator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ConcreteIterator : Iterator
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        private ConcreteAggregate concreteAggregate;
10InBlock.gif        private int current = 0;
11InBlock.gif
12InBlock.gif        public ConcreteIterator(ConcreteAggregate t)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            concreteAggregate = t;  
15ExpandedSubBlockEnd.gif        }

16InBlock.gif
17ContractedSubBlock.gifExpandedSubBlockStart.gif        Iterator 成员#region Iterator 成员
18InBlock.gif
19InBlock.gif        public object First()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            return concreteAggregate[0];
22ExpandedSubBlockEnd.gif        }

23InBlock.gif
24InBlock.gif        public object Next()
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            if (current < concreteAggregate.Count - 1)
27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
28InBlock.gif                return concreteAggregate[current++];
29ExpandedSubBlockEnd.gif            }

30InBlock.gif            return null;
31ExpandedSubBlockEnd.gif        }

32InBlock.gif
33InBlock.gif        public bool isDone()
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            return current >= concreteAggregate.Count ? true : false;
36ExpandedSubBlockEnd.gif        }

37InBlock.gif
38InBlock.gif        public object Current()
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40InBlock.gif            return concreteAggregate[current];
41ExpandedSubBlockEnd.gif        }

42InBlock.gif
43ExpandedSubBlockEnd.gif        #endregion

44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gif            ConcreteAggregate a = new ConcreteAggregate();
 2None.gif            a[0= "0";
 3None.gif            a[1= "00";
 4None.gif            a[2= "000";
 5None.gif            a[3= "0000";
 6None.gif            a[4= "00000";
 7None.gif            a[5= "000000";
 8None.gif
 9None.gif            ConcreteIterator i = new ConcreteIterator(a);
10None.gif
11None.gif            Console.WriteLine("begin");
12None.gif
13None.gif            object item = i.First();
14None.gif
15None.gif            while (item != null)
16ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
17InBlock.gif                Console.WriteLine(item);
18InBlock.gif                item = i.Next();
19ExpandedBlockEnd.gif            }

20None.gif
21None.gif            Console.ReadKey();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值