Design Patterns - Iterator Pattern

Iterator Pattern - Iterator Pattern

http://blog.csdn.net/lovelion/article/details/9992679

My understanding: The purpose of the iterator is to separate the traversal of data from the aggregation class, which is only responsible for adding, deleting, and displaying.

Separate the behavior of traversing data from the aggregate object and encapsulate it in an object called "iterator". The iterator provides the behavior of traversing the internal data of the aggregate object, which simplifies the design of the aggregate object and is more in line with the Requirements of the "Single Responsibility Principle"

Iterator Pattern : Provides a way to access aggregate objects without exposing the internal representation of the object, aliased as Cursor . The iterator pattern is an object behavior pattern.

       ● Abstract iterator: It defines the interface for accessing and traversing elements, and declares methods for traversing data elements, such as: first() method to get the first element, next() to access the next element method, hasNext() method for judging whether there is the next element, currentItem() method for obtaining the current element,etc. These methods will be implemented in the specific iterator.

       ● Concrete iterator: It implements the abstract iterator interface, completes the traversal of the aggregate object, and records the current position in the aggregate object through the cursor in the concrete iterator. In the concrete implementation, the cursor is usually a representation A non-negative integer for the position.

       ● Abstract aggregate class: It is used to store and manage element objects, declare a createIterator() method to create an iterator object, and act as an abstract iterator factory.

       ● Concrete aggregate class: It implements the createIterator() method declared in the abstract aggregate class , which returns a ConcreteIterator instancecorresponding to the concrete aggregate class


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852165&siteId=291194637
Recommended