Iterator iterator Mode (behavioral mode)
Motive (motivation)
In the process of software building, the internal structure of the collection object is often different. But for these collection objects, we want to allow external client code to transparently access the elements it contains while not exposing its internal structure, and this "transparent variable" provides the possibility that the same algorithm operates on multiple collection objects.
Using object-oriented techniques to abstract this traversal mechanism into an "iterator object" provides an elegant way of "coping with changing collection objects."
Intentions (Intent)
Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. --"Design pattern" GoF
Structure (Structure)
Sample code
Abstract classIterator// abstract iterator { Public Abstract ObjectFirst (); Public Abstract ObjectNext (); Public Abstract BOOLIsDone (); Public Abstract ObjectCurrentItem (); } Abstract classAggregate// Abstract Aggregation class { Public AbstractIterator Createiterator ();//Creating Iterators } classconcreteaggregate:aggregate// Specific aggregation class {Privateilist<Object> items=Newlist<Object>(); Public OverrideIterator Createiterator () {return NewConcreteiterator ( This); } Public intCount {Get{returnitems. Count; } } //declaring an indexer Public Object This[intIndex] { Get{returnItems[index];} Set{items. Insert (Index,value);} } } classconcreteiterator:iterator// specific iterator {Privateconcreteaggregate Aggregate; Private intCurrent =0; PublicConcreteiterator (concreteaggregate aggregate)//when initializing, the specific clustered object is passed in { This. aggregate =aggregate; } Public Override ObjectFirst () {returnaggregate[0];//get the first object to gather } Public Override ObjectNext () {ObjectRET =NULL; Current++; if(Current <aggregate. Count) {ret=Aggregate[current]; } returnret; } Public Override BOOLIsDone () {returnCurrent >=aggregate. Count; } Public Override ObjectCurrentItem () {returnAggregate[current]; } }
Client calls:
Static voidMain (string[] args) {Concreteaggregate a=Newconcreteaggregate (); a[0] ="Big Bird"; a[1] ="Side Dishes"; a[2] ="Sofa"; a[3] ="Bench"; Iterator Iterator=NewConcreteiterator (a); while(!iterator. IsDone ()) {Console.WriteLine (iterator. CurrentItem ()); Iterator. Next (); } console.readkey (); }
Output:
Big Bird
Plutella
Sofa
Bench
Several points of iterator model
- Iterative abstraction: Accesses the interior of an aggregated object without exposing its internal representation.
- Iterative polymorphism: Provides a unified interface for traversing different collection structures, enabling the same algorithm to operate on different sets of structures.
- The robustness of an iterator is considered: traversing the same time as changing the collection structure where the iterator is located can cause problems.
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
Design mode 17:iterator iterator mode (behavioral mode)