Iterator Mode:Provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
Python provides built-in support for this mode, so you do not need to write it yourself,
# Encoding = UTF-8 ## by Panda # iterator mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # iterator abstract class iterator: def first (Self): Pass def next (Self): Pass def isdone (Self): Pass def currentitem (Self ): pass # Set abstract class aggregate: def createiterator (Self): pass # specific iterator class: Class concreteiterator (iterator ): aggregate = none current = 0 def _ init _ (self, aggregate): Self. aggregate = aggrega Te self. current = 0 def first (Self): return self. aggregate [0] def next (Self): ret = none self. current + = 1 If (self. current <Len (self. aggregate): ret = self. aggregate [self. current] return ret def isdone (Self): If (self. current <Len (self. aggregate): Return false else: Return true def currentitem (Self): ret = none if (self. current <Len (self. aggregate): ret = self. aggregate [self. current] Return re T # specific collection class concreteaggregate (aggregate): items = none def _ init _ (Self): Self. items = [] def clientui (): A = concreteaggregate (). items. append ('Big Bird'). items. append ('food'). items. append ('luggage '). items. append ('foreigner '). items. append ('bus internal employee '). items. append ('thief ') printinfo (' --------- iterator mode ------------- ') I = concreteiterator (. items) item = I. first () while (false = I. isdone (): printinfo ("% s buy a ticket! "% I. currentitem (); I. next () printinfo ('\ n --------- internal Python iteration -------------') for item in. items: printinfo ("% s buy a ticket! "% Item); returnif _ name _ = '_ main _': clientui ();
Class Diagram: