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

本文介绍了一种使用C#实现的迭代器模式,包括抽象聚合接口、具体聚合类、抽象迭代器接口及其实现的具体迭代器类。展示了如何通过迭代器遍历聚合对象集合。
ContractedBlock.gifExpandedBlockStart.gifAggregate
 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.gifExpandedBlockStart.gifConcreteAggregate
 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.gifExpandedBlockStart.gifIterator
 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.gifExpandedBlockStart.gifConcreteIterator
 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.gifExpandedBlockStart.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();
可以访问聚合对象内容无需暴露内部表示设计模式是迭代器模式。迭代器模式提供一个对象顺序访问聚合对象中的一系列数据,而暴露聚合对象内部表示,是一种对象行为型模式[^2]。 该模式包含聚合对象和迭代器两个主要部分。聚合对象负责存储数据,迭代器负责遍历这些数据。以下是一个简单的 Python 代码示例,展示了迭代器模式的基本实现: ```python # 自定义集合 class MyCollection: def __init__(self, items): self.items = items def create_iterator(self): return MyIterator(self.items) # 自定义迭代器 class MyIterator: def __init__(self, items): self.items = items self.index = 0 def has_next(self): return self.index < len(self.items) def next(self): if self.has_next(): item = self.items[self.index] self.index += 1 return item return None # 使用示例 collection = MyCollection([1, 2, 3, 4, 5]) iterator = collection.create_iterator() while iterator.has_next(): print(iterator.next()) ``` 上述代码中,`MyCollection` 类是聚合对象,它包含了一个数据列表 `items`,并提供了 `create_iterator` 方法来创建一个迭代器对象。`MyIterator` 类是迭代器,它实现了 `has_next` 和 `next` 方法,用于遍历 `MyCollection` 中的元素。客户端代码通过迭代器对象访问 `MyCollection` 中的元素,而需要了解 `MyCollection` 的内部结构,实现了访问聚合对象内容暴露内部表示的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值