I. Introduction
In my previous blog post, I shared my understanding of the command mode. The command mode mainly abstracts the behavior into commands, making the behavior of the requester and the behavior of the recipient form a low coupling. In this chapter, we will introduce the iterator mode. I will not talk about the following nonsense. I will go directly to the topic of this blog post.
II. Introduction to the iterator Mode
The iterator is generated for the Collection object. For the collection object, it must involve the addition and deletion operations on the collection element. At the same time, it must support traversing the collection element, we can also put the traversal operation in the Set object at this time, but in this case, the set object will assume too many responsibilities. One of the object-oriented design principles is a single responsibility principle, therefore, we should try our best to separate these responsibilities and use different classes to assume different responsibilities. The iterator mode uses the iterator class to traverse the set elements.
2.1 Definition of the iterator Mode
The iterator mode provides a way to access each element of an aggregate object in sequence (understood as a collection object) without exposing the internal representation of the object, in this way, the internal structure of the set is not exposed, and external code can transparently access the data in the set.
2.2 structure of the iterator Mode
Since the iterator mode is responsible for traversing the set objects, there are two classes in this mode, one is the aggregation class and the other is the iterator class. In the object-oriented principles, there is also an interface-oriented programming. Therefore, in the iterator mode, two interfaces are abstracted, one is an aggregate interface and the other is an iterator interface, in this way, there are four roles in the iterator mode. The specific class diagram is as follows:
The iterator mode consists of the following roles:
- Iterator: The iterator role defines interfaces for accessing and traversing elements.
- Concrete iteraror: The iterator role implements the iterator interface and records the current position in the traversal.
- Aggregate: The aggregate role defines the interface for obtaining the iterator role.
- Concrete aggregate: a specific aggregate role implements an aggregate role interface.
2.3 Implementation of the iterator Mode
After introducing the iterator mode, let's take a look at the implementation of the iterator mode. The specific implementation code is as follows:
1 // abstract aggregation Class 2 public interface ilistcollection 3 {4 iterator getiterator (); 5} 6 7 // iterator abstract class 8 public interface iterator 9 {10 bool movenext (); 11 object getcurrent (); 12 void next (); 13 void reset (); 14} 15 16 // specific aggregation Class 17 public class concretelist: ilistcollection 18 {19 int [] collection; 20 public concretelist () 21 {22 collection = new int [] {2, 4, 6, 8 }; 23} 24 25 public iterator getiterator () 26 {27 return New concreteiterator (this); 28} 29 30 public int length 31 {32 get {return collection. length;} 33} 34 35 public int getelement (INT index) 36 {37 return Collection [Index]; 38} 39} 40 41 // specific iterator class 42 public class concreteiterator: iterator 43 {44 // to traverse the collection object, the iterator naturally needs to reference the collection object 45 private concretelist _ list; 46 private int _ index; 47 48 Public concreteiterator (concretelist list) 49 {50 _ list = List; 51 _ Index = 0; 52} 53 54 55 public bool movenext () 56 {57 if (_ index <_ list. length) 58 {59 return true; 60} 61 Return false; 62} 63 64 public object getcurrent () 65 {66 return _ list. getelement (_ index); 67} 68 69 public void reset () 70 {71 _ Index = 0; 72} 73 74 public void next () 75 {76 if (_ index <_ list. length) 77 {78 _ index ++; 79} 80 81} 82} 83 84 // client 85 class program 86 {87 static void main (string [] ARGs) 88 {89 iterator; 90 ilistcollection list = new concretelist (); 91 iterator = List. getiterator (); 92 93 while (iterator. movenext () 94 {95 int I = (INT) iterator. getcurrent (); 96 console. writeline (I. tostring (); 97 iterator. next (); 98} 99 100 console. read (); 101} 102}
Naturally, the running result of the above Code is also the output of each element in the set. The specific running result is shown in:
Iii. Application of the iterator mode in. net
In. net, the clustering interface and the iterator interface in the iterator mode already exist. The ienumerator interface plays the iterator role, and the ienumberable interface plays the abstract aggregation role, there is only one getenumerator () method. You can refer to msdn for the definitions of these two interfaces. In. in. NET 1.0 ,. many collections in the. NET class library have already implemented the iterator mode. You can use the decompilation tool reflector to view the system under the mscorlib assembly. classes in the collections namespace. The arraylist definition code is provided here. You can use the decompilation tool to view the specific implementation code. The specific code is as follows:
1 public class arraylist: ilist, icollection, ienumerable, icloneable 2 {3 // fields 4 private const int _ defaultcapacity = 4; 5 private object [] _ items; 6 private int _ size; 7 [nonserialized] 8 private object _ syncroot; 9 private int _ version; 10 Private Static readonly object [] emptyarray; 11 12 Public Virtual ienumeratorGetenumerator(); 13 Public Virtual ienumeratorGetenumerator(INT index, int count); 14 15 // properties16 Public Virtual int capacity {Get; set;} 17 Public Virtual int count {Get ;} 18 .............. // use The Decompilation tool reflector to view more code. 19}
By viewing the source code, you can find that the implementation of the iterator in arraylist is very similar to the sample code we provided earlier. However, in. NET 2.0, with the yield return keyword, it is easier to implement the iterator mode. For more information about the iterator, see my post.
Iv. application scenarios of the iterator Mode
In the following cases, you can consider using the iterator mode:
- The system needs to access the content of an aggregate object without exposing its internal representation.
- The system must support multiple traversal of aggregate objects.
- The system must provide a unified interface for different aggregation structures.
V. Advantages and disadvantages of the iterator Mode
Since the iterator assumes the responsibility of traversing the set, it has the following advantages:
- The iterator mode allows you to access the content of an aggregate object without exposing its internal representation, that is, iterative abstraction.
- The iterator mode provides a unified interface for Traversing different set structures, so that the same algorithm can be operated on different set structures.
Defects in the iterator mode:
- The iterator mode changes the structure of the set where the iterator is located while traversing, leading to exceptions. Therefore, the foreach statement can only be used to traverse the set, and the elements in the set cannot be modified at the same time.
Vi. Summary
The content in this blog is over. The iterator mode abstracts an iterator class to separate the traversal behavior of the collection object, in this way, the internal structure of the set is not exposed, and external code can transparently access the data in the set. The observer mode will be introduced in a blog.
C # design pattern (16) -- iterator Pattern)