Iterator Pattern)

Source: Internet
Author: User

Iterator Pattern)
1. What is the iterator mode? The iterator is used to encapsulate the traversal details of the Set object, so that the caller can use a unified interface to implement the traversal iterator of the set object. It also provides some protection for the set object and wants to traverse the set, just call the iterator method directly. We do not know or need to know the internal data management method of the set. for example, if there is a set of custom objects, we don't know what set objects they are used for maintenance. They may be Array, List, or others. Our job is to traverse this set of objects (or add some additional processing on the basis of traversal). How can we achieve this? First, judge the type of the Set object, and then perform different processing for each different set type, we need a different loop for processing, although these loop blocks only have slight differences, but we also have to provide a loop for processing .. If there are 100 different collection types, we may have to use 100 loops that are similar to each other to achieve traversal. There is no doubt a large amount of redundant code, so we need a unified entrance, if we want to turn n cycles into a well-adaptive loop, the iterator is the unified entry we are thinking about: the iterator interface + View Code also needs to implement some specific iterators to traverse details: Array iterator: + View CodeP. s. note the specific implementation of the hasNext method of the Array iterator List iterator: + View Code with these specific iterators, we only need a loop to get it done: + View Code 3. java's support for Iterator is due to the convenience and necessity of the Iterator. Java then provides support for Iterator. Currently, many Set objects support Iterator, such as Set, List, Map, SortedSet, sortedMap, HashSet, TreeSet, ArrayList, sorted list, and Vector are the only arrays that do not support iterators (except stack and queue ), the above Implementation of the custom Iterator interface, in fact, Java itself provides this interface (java. util. iterator), but the difference is: Java. util. the Iterator interface defines three methods (except the hasNext and next methods): If the collection object is unwilling to provide the remove method, it does not matter. We can throw an exception to reject the implementation, like this: The Calling method of the + View Code iterator is completely unchanged: + View Code 4. many languages support the for each (or for in) loop. The internal implementation of the iterator is to call the iterator to complete the traversal. Of course, it is just a little bit of knowledge, more importantly, learn the design principles of the iterator and define its own iterator 5 as needed. the extended iterator mode is simple, and we don't even need to define our own iterator (the iterator interface provided by the API can almost meet our needs)

Related Article

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.