一、引言
今天我们开始讲“行为型”设计模式的第三个模式,该模式是【迭代器模式】,英文名称是:Iterator Pattern。还是老套路,先从名字上来看看。“迭代器模式”我第一次看到这个名称,我的理解是,迭代是遍历的意思,迭代器可以理解为是遍历某某的工具,遍历什么呢?在软件设计中,当然遍历的是集合对象,所以说迭代器模式是遍历集合的一种通用的算法。如果集合只有一种类型,那这个模式就没用了,就是因为集合对象包含数组、列表,字典和哈希表等各种对象,如果为每一种集合对象都实现一套遍历算法,也不太现实,因此为了解决遍历集合有一个统一的接口这个事情,所以就提出了“迭代器”这个模式。
二、迭代器模式的详细介绍
2.1、动机(Motivate)
在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供了可能。
使用面向对象技术将这种遍历机制抽象为“迭代器对象”为“应对变化中的集合对象”提供了一种优雅的方式。
2.2、意图(Intent)
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。 ——《设计模式》GoF
2.3、结构图
2.4、模式的组成
从迭代器模式的结构图可以看出,它涉及到四个角色,它们分别是:
(1)、抽象迭代器(Iterator):抽象迭代器定义了访问和遍历元素的接口,一般声明如下方法:用于获取第一个元素的first(),用于访问下一个元素的next(),用于判断是否还有下一个元素的hasNext(),用于获取当前元素的currentItem(),在其子类中将实现这些方法。
(2)、具体迭代器(ConcreteIterator):具体迭代器实现了抽象迭代器接口,完成对集合对象的遍历,同时在对聚合进行遍历时跟踪其当前位置。
(3)、抽象聚合类(Aggregate):抽象聚合类用于存储对象,并定义创建相应迭代器对象的接口,声明一个createIterator()方法用于创建一个迭代器对象。
(4)、具体聚合类(ConcreteAggregate):具体聚合类实现了创建相应迭代器的接口,实现了在抽象聚合类中声明的createIterator()方法,并返回一个与该具体聚合相对应的具体迭代器ConcreteIterator实例。
2.5、迭代器模式的代码实现
迭代器模式在现实生活中也有类似的例子,比如:在部队中,我们可以让某一队伍当中的某人出列,或者让队列里面的每个人依次报名,其实这个过程就是一个遍历的过程。没什么可说的,具体实现代码如下:
1 namespace 迭代器模式的实现 2 { 3 // 部队队列的抽象聚合类--该类型相当于抽象聚合类Aggregate 4 public interface ITroopQueue 5 { 6 Iterator GetIterator(); 7 } 8 9 迭代器抽象类 10 Iterator 11 12 bool MoveNext(); 13 Object GetCurrent(); 14 void Next(); 15 Reset(); 16 17 18 部队队列具体聚合类--相当于具体聚合类ConcreteAggregate 19 sealed class ConcreteTroopQueue:ITroopQueue 20 21 private string[] collection; 22 23 public ConcreteTroopQueue() 24 { 25 collection = new string[] { "黄飞鸿",方世玉洪熙官严咏春" }; 26 } 27 28 Iterator GetIterator() 29 30 return new ConcreteIterator(this); 31 32 33 int Length 34 35 get { return collection.Length; } 36 37 38 string GetElement( index) 39 40 collection[index]; 41 42 43 44 具体迭代器类 45 ConcreteIterator : Iterator 46 47 迭代器要集合对象进行遍历操作,自然就需要引用集合对象 48 private ConcreteTroopQueue _list; 49 _index; 50 51 ConcreteIterator(ConcreteTroopQueue list) 52 53 _list = list; 54 _index = 0; 55 56 57 MoveNext() 58 59 if (_index < _list.Length) 60 { 61 true 62 } 63 false 64 65 66 Object GetCurrent() 67 68 _list.GetElement(_index); 69 70 71 Reset() 72 73 _index = 74 75 76 Next() 77 78 79 80 _index++ 81 82 83 84 85 86 客户端(Client) 87 Program 88 89 static void Main([] args) 90 91 Iterator iterator; 92 ITroopQueue list = new ConcreteTroopQueue(); 93 iterator = list.GetIterator(); 94 95 while (iterator.MoveNext()) 96 97 string ren = ()iterator.GetCurrent(); 98 Console.WriteLine(ren); 99 iterator.Next(); 100 101 102 Console.Read(); 103 104 105 }
三、迭代器模式的实现要点:
1、迭代抽象:访问一个聚合对象的内容而无需暴露它的内部表示。
2、迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。
3、迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。
3.1】、迭代器模式的优点:
(1)、迭代器模式使得访问一个聚合对象的内容而无需暴露它的内部表示,即迭代抽象。
(2)、迭代器模式为遍历不同的集合结构提供了一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作
3.2】、迭代器模式的缺点:
(1)、迭代器模式在遍历的同时更改迭代器所在的集合结构会导致出现异常。所以使用foreach语句只能在对集合进行遍历,不能在遍历的同时更改集合中的元素。
3.3】、迭代器模式的使用场景:
(1)、访问一个聚合对象的内容而无需暴露它的内部表示。
(2)、支持对聚合对象的多种遍历。
(3)、为遍历不同的聚合结构提供一个统一的接口(即,支持多态迭代)。
四、.NET 中迭代器模式的实现
在mscorlib程序集里有这样一个命名空间,该命名空间就是:System.Collections,在该命名空间里面早已有了迭代器模式的实现。对于聚集接口和迭代器接口已经存在了,其中IEnumerator扮演的就是迭代器的角色,它的实现如下:
IEnumerator { object Current { get; } MoveNext(); Reset(); }
属性Current返回当前集合中的元素,Reset()方法恢复初始化指向的位置,MoveNext()方法返回值true表示迭代器成功前进到集合中的下一个元素,返回值false表示已经位于集合的末尾。能够提供元素遍历的集合对象,在.Net中都实现了IEnumerator接口。
IEnumerable则扮演的就是抽象聚集的角色,只有一个GetEnumerator()方法,如果集合对象需要具备跌代遍历的功能,就必须实现该接口。
IEnumerable
{
IEumerator GetEnumerator();
}
抽象聚合角色(Aggregate)和抽象迭代器角色(Iterator)分别是IEnumerable接口和IEnumerator接口,具体聚合角色(ConcreteAggregate)有Queue类型, BitArray等类型,代码如下:
1 BitArray : ICollection,IEnumerable,ICloneable 3 [Serializable] 4 BitArrayEnumeratorSimple : IEnumerator,1)"> 6 BitArray bitarray; 7 8 index; 9 10 version; 11 12 currentElement; 13 14 virtual Current 15 16 get 17 { 18 if (this.index == -1) 19 { 20 throw new InvalidOperationException(Environment.GetResourceString(InvalidOperation_EnumNotStarted)); 21 } 22 this.index >= .bitarray.Count) 23 24 InvalidOperation_EnumEnded 25 26 .currentElement; 27 } 28 29 internal BitArrayEnumeratorSimple(BitArray bitarray) 32 this.bitarray = bitarray; 33 this.index = - 34 this.version = bitarray._version; 35 36 37 Clone() 38 39 base.MemberwiseClone(); 40 41 42 43 44 this.version != .bitarray._version) 45 46 InvalidOperation_EnumFailedVersion 47 48 this.index < this.bitarray.Count - 49 50 this.index++ 51 this.currentElement = this.bitarray.Get(.index); 52 53 54 this.index = .bitarray.Count; 55 56 57 58 59 60 61 62 63 64 65 66 67 68 const int BitsPerInt32 = 32 69 70 int BytesPerInt32 = 4 71 72 int BitsPerByte = 8 73 74 [] m_array; 75 m_length; 77 78 _version; 79 80 [NonSerialized] 81 _syncRoot; 82 83 int _ShrinkThreshold = 256 84 85 [__DynamicallyInvokable] 86 bool this[ index] 87 [__DynamicallyInvokable] 89 91 .Get(index); 92 93 94 set 95 96 .Set(index,value); 97 99 101 104 105 106 .m_length; 107 108 109 110 111 if (value < 112 113 new ArgumentOutOfRangeException(valueArgumentOutOfRange_NeednonNegNum114 115 int arrayLength = BitArray.GetArrayLength(value,1)">116 if (arrayLength > this.m_array.Length || arrayLength + 256 < .m_array.Length) 117 118 int[] array = [arrayLength]; 119 Array.copy(this.m_array,array,(arrayLength > this.m_array.Length) ? .m_array.Length : arrayLength); 120 this.m_array = array; 121 122 if (value > .m_length) 123 124 int num = BitArray.GetArrayLength(this.m_length,1)">32) - 125 int num2 = this.m_length % 126 if (num2 > 127 128 this.m_array[num] &= (1 << num2) - 129 130 Array.Clear(1,arrayLength - num - 131 132 this.m_length = value; 133 this._version++134 135 136 137 Count 138 139 140 141 142 143 144 145 SyncRoot 146 147 148 149 this._syncRoot == null150 151 Interlocked.CompareExchange<object>(ref this._syncRoot,object(),1)">152 153 ._syncRoot; 154 155 156 157 IsReadOnly 158 159 160 161 162 163 164 165 IsSynchronized 166 167 168 169 170 171 172 173 BitArray() 174 175 176 177 178 public BitArray(int length) : this(length,1)">179 180 181 182 183 int length,1)"> defaultValue) 184 185 if (length < 186 187 length188 189 this.m_array = int[BitArray.GetArrayLength(length,1)">)]; 190 length; 191 int num = defaultValue ? -1 : 192 for (int i = 0; i < this.m_array.Length; i++193 194 this.m_array[i] = num; 195 196 this._version = 197 198 199 200 byte[] bytes) 201 202 if (bytes == 203 204 new ArgumentNullException(bytes205 206 if (bytes.Length > 268435455207 208 new ArgumentException(Environment.GetResourceString(Argument_ArrayTooLarge[] 209 210 8 211 }),1)">212 213 int[BitArray.GetArrayLength(bytes.Length,1)">214 this.m_length = bytes.Length * 215 int num = 216 int num2 = 217 while (bytes.Length - num2 >= 218 219 this.m_array[num++] = ((int)(bytes[num2] & 255) | (int)(bytes[num2 + 1] & 255) << 8 | (2] & 16 | (3] & 24220 num2 += 221 222 switch (bytes.Length - num2) 223 224 case : 225 goto IL_103; 226 2227 break228 3229 this.m_array[num] = (16230 231 default232 IL_11C; 233 234 this.m_array[num] |= (235 IL_103: 236 255237 IL_11C: 238 239 240 241 242 [] values) 243 244 if (values == 245 246 values247 248 int[BitArray.GetArrayLength(values.Length,1)">249 values.Length; 250 0; i < values.Length; i++251 252 if (values[i]) 253 254 this.m_array[i / 32] |= 1 << i % 255 256 257 258 259 260 261 262 263 264 265 266 267 if (values.Length > 67108863268 269 270 271 32 272 }),1)">273 274 [values.Length]; 275 this.m_length = values.Length * 276 Array.copy(values,1)">.m_array,values.Length); 277 278 279 280 281 BitArray(BitArray bits) 282 283 if (bits == 284 285 bits286 287 int arrayLength = BitArray.GetArrayLength(bits.m_length,1)">288 289 bits.m_length; 290 Array.copy(bits.m_array,arrayLength); 291 this._version = bits._version; 292 293 294 295 bool Get(296 297 if (index < 0 || index >= .Length) 298 299 indexArgumentOutOfRange_Index300 301 return (this.m_array[index / 32] & 1 << index % 32) != 302 303 304 305 void Set(int index,1)"> value) 306 307 308 309 310 311 (value) 312 313 314 315 else 316 317 32] &= ~(318 319 320 321 322 323 void SetAll(324 325 int num = value ? -326 int arrayLength = BitArray.GetArrayLength(327 0; i < arrayLength; i++328 329 330 331 332 333 334 335 BitArray And(BitArray value) 336 337 if (value == 338 339 340 341 this.Length != value.Length) 342 343 Arg_ArrayLengthsDiffer344 345 346 347 348 this.m_array[i] &= value.m_array[i]; 349 350 351 352 353 354 355 BitArray Or(BitArray value) 356 357 358 359 360 361 362 363 364 365 366 367 368 this.m_array[i] |=369 370 371 372 373 374 375 BitArray Xor(BitArray value) 376 377 378 379 380 381 382 383 384 385 386 387 388 this.m_array[i] ^=389 390 391 392 393 394 395 BitArray Not() 396 397 398 399 400 this.m_array[i] = ~.m_array[i]; 401 402 403 404 405 406 void copyTo(Array array,1)">407 408 if (array == 409 410 array411 412 413 414 415 416 if (array.Rank != 417 418 Arg_RankMultiDimNotSupported419 420 if (array is []) 421 422 Array.copy(0,index,BitArray.GetArrayLength(423 424 425 426 427 428 if (array.Length - index < arrayLength) 429 430 Argument_InvalidOffLen431 432 byte[] array2 = ([])array; 433 434 435 array2[index + i] = (byte)(4] >> i % 4 * 8 & 436 437 438 439 440 441 if (!(array [])) 442 443 Arg_BitArrayTypeUnsupported444 445 if (array.Length - index < 446 447 448 449 bool[] array3 = (450 int j = 0; j < this.m_length; j++451 452 array3[index + j] = ((this.m_array[j / 32] >> j % 32 & 1) != 453 454 455 456 457 458 459 460 new BitArray(.m_array) 461 462 _version = ._version,463 m_length = .m_length 464 }; 465 466 467 468 IEnumerator GetEnumerator() 469 470 new BitArray.BitArrayEnumeratorSimple(471 472 473 int GetArrayLength(int n,1)"> div) 474 475 if (n <= 476 477 return 478 479 return (n - 1) / div + 480 481 }
还有很多类型,就不一一列举了,大家可以查看源代码,每个元素都可以在迭代器模式的构造图上找到对应的元素。
具体的类型和代码截图如下:
五、总结
今天到此就把迭代器模式写完了,该模式不是很难,结构也不是很复杂,Net框架里面也有现成的实现。并且在 Net 2.0里面还有升级的实现,要想学习该模式,可以好好看看该模式在Net 框架中的实现,受益匪浅。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。