The design pattern of the diamondback--iterator mode

Source: Internet
Author: User
Tags iterable


background

Iteration is a process of traversal, since is the traversal of nature is everywhere, for example, in the street to see the beauty of the time, always a do not let go, a panoramic view, but, to tell the truth, in the summer to see beauty, in fact, I was refused, do not say, Nosebleed has to leave out


1. Intention to use

Traverse each object


2. Life Example

The conductor of the bus is checking the tickets and checking the ticket for every passenger.


3. Java Example (framework, JDK, JEE)

The top-level interface of the collection collection is the implementation of the interface iterable, he is an iterator pattern encapsulation, iterable only one method,iterator<t> Iterator (); However, iterator declares the method of the corresponding iteration, which is as follows:

Boolean hasnext (); E next (); void remove ();

In fact, whether we use a map or a list, we sometimes go through the deletion of the member, this time the iterator is the best, because the normal for is not to delete members in the iteration.


4. Pattern class Diagram

Aggregate: Abstract member role, which is the object member to traverse

Concreteaggregate: The specific member role, the specific implementation of aggregate, in fact, the aggregation object is a collection or say array

Iterator: Abstract iterator, he declares the tool method of the iteration

Concreteiterator: The concrete iterator, he implements the iterative tool method, can have a variety of different traversal implementations


5. Advantages of the Model

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

You should consider using the iterator pattern when you need to access a clustered object, and regardless of what these objects need to traverse.

You might consider using an iterator pattern when you need to traverse a clustered object in several ways.

Provides a unified interface for traversing different aggregation structures, such as start, next, end, current, and so on.

An iterator pattern is the separation of the traversal behavior of a collection object, which is responsible for abstracting an iterator class that does not expose the internal structure of the collection, but also allows the external code to transparently access the internal data of the collection


6. Comparison with similar models

After reading this design pattern, I feel the iterator pattern is marvellous, this pattern is everywhere ah, and in Java for Loop is a embodiment, traversal is a variety of words, but the iterator pattern is really one of the principles.


The design pattern of the diamondback--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.