python-iterator mode

Source: Internet
Author: User

Description

A collection is a data structure that is used to manage and organize objects. Set has two basic functions: first, bulk storage of data objects, second, without exposing the internal structure of the set of conditions to provide access to the internal elements of the interface (possible traversal mode: sequence, reverse-order traversal, the breadth of the binary tree, first order sequence traversal, etc.). To keep the collection neat and elegant, instead of saying that the collection contains a variety of traversal methods inside. Therefore, it is required to separate the traversal method from the collection's accusation and encapsulate it separately as an iterator that iterates through the collection's internal data, according to different traversal requirements. This idea minimizes the degree of coupling between each other, thus creating a loosely coupled object network. The main point of separation of duties is to encapsulate the responsibilities of separation and establish relationships with each other in the form of abstract objects.

Iterator mode: Provides a way to sequentially access individual elements in an aggregated object without exposing the object's internal representation

Two responsibilities for aggregating objects: storing data, the basic responsibility of aggregated objects traverse the data, which is both changeable and separable. The behavior of traversing data is detached from the aggregation object and encapsulated in an iterator object. The iterator provides the behavior of iterating through the internal data of the aggregated object, simplifying the design of the aggregation object and more in line with the principle of single responsibility.

The iterator pattern consists of the following 4 roles:

Iterator (abstract iterator) Concreteiterator (concrete iterator) Aggregate (Abstract aggregation Class) Concreteaggregate (Specific aggregation Class)

Instance:
#iterator abstract classclassIterator (object):defFirst (self):Pass    defNext (self):Pass    defIsdone (self):Pass    defCurritem (self):Pass#aggregating abstract ClassesclassAggregate (object):defCreateiterator (self):Pass#Specific iterator ClassesclassConcreteiterator (Iterator):def __init__(self, aggregate): Self.aggregate=Aggregate Self.curr=0defFirst (self):returnSelf.aggregate[0]defNext (self): ret=None Self.curr+ = 1ifSelf.curr <Len (self.aggregate): Ret=Self.aggregate[self.curr]returnretdefIsdone (self):returnTrueifSelf.curr+1 >= Len (self.aggregate)ElseFalsedefCurritem (self):returnSelf.aggregate[self.curr]#Specific aggregation classesclassconcreteaggregate (Aggregate):def __init__(self): self.ilist= []    defCreateiterator (self):returnconcreteiterator (self)classConcreteiteratordesc (Iterator):def __init__(self, aggregate): Self.aggregate=Aggregate Self.curr= Len (aggregate)-1defFirst (self):returnSelf.aggregate[-1]    defNext (self): ret=None Self.curr-= 1ifSelf.curr >=0:ret=Self.aggregate[self.curr]returnretdefIsdone (self):returnTrueifSelf.curr-1<0ElseFalsedefCurritem (self):returnSelf.aggregate[self.curr]if __name__=="__main__": CA=concreteaggregate () ca.ilist.append ("Big Bird") Ca.ilist.append ("Side Dishes") Ca.ilist.append ("Foreigner") Ca.ilist.append ("thief") Itor=concreteiterator (ca.ilist)Print(Itor. First ()) while  notItor. Isdone ():Print(Itor. Next ())Print("———— Reverse ————") Itordesc=Concreteiteratordesc (ca.ilist)Print(Itordesc. First ()) while  notItordesc. Isdone ():Print(Itordesc. Next ())

#源码出处http://www.cnblogs.com/onepiece-andy/p/python-iterator-pattern.html

Printing results:

Big Bird
Plutella
Foreigners
Thief
———— Reverse ————
Thief
Foreigners
Plutella
Big Bird

Pattern Benefits

Supports traversing an aggregate object in a different way, it is possible to define multiple traversal methods on the same aggregate object to simplify the aggregation class because of the introduction of the abstraction layer, it is convenient to add new aggregation classes and iterator classes, without modifying the original code, conforming to the open and closed principle

Pattern disadvantage

When adding new aggregation classes, it is necessary to add new iterator classes Accordingly, the number of classes increases, which increases the complexity of the system to some extent, the design of the abstract iterator is more difficult, and the future expansion of the system needs to be fully considered. It's not easy to create a fully-considered abstract iterator when customizing iterators

Mode applicable environment

Accessing the contents of an aggregated object without exposing its internal representation requires a multiple traversal of an aggregate object to provide a unified interface for traversing different aggregation structures, providing different traversal modes for different aggregation structures in the implementation class of the interface, while the client can manipulate the interface consistently

python-iterator mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.