Transferred from: http://blog.csdn.net/lilu_leo/article/details/7609496
Overview
iterator (Iterator) mode, also known as cursor mode. GOF provides a way to access individual elements of a container (container) object without exposing the object's internal details.
In object-oriented software design, we often encounter a class of collection objects, the internal structure of such a collection object may have a variety of implementations, but it comes down to two points that we need to care about: one is the data storage structure inside the collection, and the second is to traverse the data inside the collection. One of the principles of object-oriented design is the single responsibility principle of a class, so we need to break down these responsibilities as much as possible, and use different classes to take on different responsibilities. The iterator pattern is the separation of the traversal behavior of the collection object and the abstraction of an iterator class to be responsible for not exposing the internal structure of the collection, but also allowing the external code to transparently access the data inside the collection.
1. In layman's terms, it is possible for others to use our container elements, but I just want him to use the elements, not to let him know what the container is like. That is, the most basic, access to each element without exposing the inner details of the container.
2. It is possible to traverse container elements in more than one way
3. Multiple traversal of the container element at the same time. Because iterators save the current traversal state, this requirement can be achieved through the iterator pattern.
Intention
Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. [GOF "design mode"]
Structure diagram
The iterator pattern structure is as follows:
Consists of the following four roles:
1) iterator Role (Iterator): The iterator role is responsible for defining the interfaces that access and traverse elements.
2) Specific iterator role (concrete Iterator): The specific iterator role is to implement the iterator interface, and to record the current position in the traversal.
3) container Role (Container): The container role is responsible for providing the interface that creates the specific iterator role.
4) Specific container role (concrete Container): The specific container role implements the interface that creates the specific iterator role-the specific iterator role is related to the structure of the container.
Code Small Example
Iterators (Iterator)
- Public interface Iterator {
- String next ();
- boolean havenext ();
- }
Specific iterator role (concrete Iterator)
- Public class Tvchannel implements iterator{
- private string[] Tvchannel ={"Cctv-1","Cctv-2","cctv-3","cctv-4","cctv-5","cctv-6", "Cctv-7"};
- private int current = 0;
- @Override
- Public String Next () {
- if (Havenext ()) {
- return tvchannel[current++];
- }
- return null;
- //TODO auto-generated method stub
- }
- @Override
- Public Boolean havenext () {
- //TODO auto-generated method stub
- if (current<tvchannel.length) {
- return true;
- }
- return false;
- }
- }
Container Role (Container)
- Public interface IContainer {
- Iterator Createriterator ();
- }
Specific container roles (concrete Container):
- Public class TV implements icontainer{
- Public TV () {
- System.out.println ("open a TV");
- }
- Public Iterator Createriterator () {
- return new Tvchannel ();
- }
- }
Test using:
- Public static void Main (string[] args) {
- //TODO auto-generated method stub
- IContainer TV = new TV ();
- Iterator Iterator = Tv.createriterator ();
- While (Iterator.havenext ()) {
- System.out.println ("Tvchennel:" +iterator.next ());
- }
- }
Why should a simple container be defined as an interface? Think about programming for the interface, not for the specific implementation of programming! If you want to expand, use the interface more convenient.
Test results:
- Open a TV
- tvchennel:cctv-1
- tvchennel:cctv-2
- tvchennel:cctv-3
- tvchennel:cctv-4
- tvchennel:cctv-5
- tvchennel:cctv-6
- tvchennel:cctv-7
Effect and key points of realization
- Iterative abstraction: Accesses the contents 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.
Summarize
The iterator pattern is the separation of the traversal behavior of the collection object and the abstraction of an iterator class to be responsible for not exposing the internal structure of the collection, but also allowing the external code to transparently access the data inside the collection.
the role of the iterator pattern:
- It supports traversing an aggregate object in different ways: complex aggregations can be traversed in many ways. The iterator pattern makes it easy to change the traversal algorithm: You only need an instance of a different iterator to replace the original instance. You can also define the subclass of the iterator yourself to support the new traversal.
- Iterators simplify the aggregation of interfaces with an iterator traversal interface, and the aggregation itself no longer requires a similar traversal interface. This simplifies the interface of the aggregation.
- On the same aggregation there can be multiple traversal of each iterator to maintain its own traversal state. So you can do multiple traversal at the same time.
- In the iterator mode, it is convenient to add new aggregation classes and iterator classes without modifying the original code to meet the requirements of the "open and close principle".
disadvantages of the iterator patternBecause the iterator pattern separates the responsibilities of storing data and traversing data, adding new aggregation classes requires corresponding additions to the new iterator classes, and the number of classes increases in pairs, which in some way increases the complexity of the system.
Where applicable
As described above, we can see that the iterator pattern brings the following benefits to the application of the container:
- Supports traversing a container role in a different way. Depending on the implementation method, there will be differences in effect.
- simplifies the interface of the container. In Java collection, however, the container provides a traversal interface for extensibility.
- Multiple traversal can be performed on the same container object. Because the traversal state is stored in each iterator object.
This can also be used to derive the scope of the iterator pattern:
- Accesses the contents of a container object without exposing its internal representation.
- Supports multiple traversal of container objects.
- Provides a unified interface for traversing different container structures (polymorphic iterations).
Iterator (Iterator) mode