6th of the design pattern-iterator mode (Java implementation)

Source: Internet
Author: User

6th of the design pattern-iterator mode (Java implementation)

 "I'm out of date, let's not talk about it, now Java comes with an iterator, what is there to say?" "Although there are already, but specific details?" Know the mechanism of implementation is not beautiful? "All right, OK." (The iterator promised down sullen.) The author eats the small cage bag, the suck smack way: Hum, wants to be lazy, the window has not ~).

Self-Introduction to the iterator pattern

As you can see, I am now in decline, basically no one will write an iterator alone, unless the product nature of the development, I defined as follows: Provide a-means to access the elements of a aggregate object sequentially Without exposing its underlying representation. What it means is to provide a way to access individual elements of a container object without exposing the object's internal details.

Iterators are services for containers, so what is a container? A container is a general designation of all types that can hold objects. For example collection collection type, set type and so on, these in the "Thinking in Java" in the Chapter 11 inside, want to learn more about the container also need to see Chapter 17, if not remember the words should be this, OK, continue, I , is to solve the traversal of these containers born, of course, these are the things of the JDK1.0.8 era, now JDK has already had the implementation of the iterator, so, I also is an outdated product ~ The following class diagram first:

I provide convenient container convenience, the container as long as the management increase or decrease can be, need to pass over to me. The following specific analysis of the class diagram:

    • Iterator abstract iterators: Abstract iterators are responsible for defining the interfaces that access and traverse elements, and there are basically three fixed methods: first () gets the second element, next () gets the next element, and IsDone () has finished accessing it. In Java is Hasnext ().
    • Concreteiterator specific iterator: implements the iterator interface to complete the container element traversal.
    • Aggregate Abstract Container: responsible for providing the creation of a specific iterator role interface.
    • Concreteaggregate Specific container: A method that implements the container interface definition for a specific container, creating an object that accommodates the iterator.

Self-analysis of the iterator pattern

Shortcomings, really no, the advantages are as follows:

    • Supports traversing an aggregation in different ways.
    • simplifies the interface for aggregation.
    • There can be multiple traversal on the same aggregation.

Implementation of Iterators

  This is not very common, then the implementation of the common code directly, the first is an abstract iterator:

1  Public Interfaceiterator{2     //traverse the next element3      Publicobject Next ();4     //whether it has reached the last element5      Public BooleanHasnext ();6     //Delete the currently pointing element7      Public Booleanremove ();8}

  Next is the specific iterator implementation code: 

1  Public classConcreteiteratorImplementsiterator{2     PrivateVector vector =NewVector ();3     //defining the current cursor4      Public intcursor = 0;5@SuppressWarnings ("Unchecked")6      Publicconcreteiterator (vector vector) {7          This. Vector =Vector;8     }9 Ten     //Judging if it's the last one One      Public BooleanHasnext () { A         if( This. cursor = = This. Vector.size ()) { -             return false; -         } the         Else -             return true; -     } -  +     //returns the next element -      PublicObject Next () { +Object result =NULL; A         if( This. Hasnext ()) { atresult = This. Vector.get ( This. cursor++); -         } -         Else -result =NULL; -  -         returnresult; in     } -  to     //Delete Current element +      Public BooleanRemove () { -          This. Vector.remove ( This. cursor); the         return true; *     } $ Panax Notoginseng  -}
View Code

 Then the abstract container class:

1  Public Interfaceaggregate{2     //is the container must have the element increase3      Public voidAdd (Object object);4     //reduce element5      Public voidremove (Object object);6     //traversed by Iterators7      PublicIterator Iterator ():8}

Finally, the implementation class for the specific container:

1  Public classConcreteaggregateImplementsaggregate{2     //Containers for Container objects3     PrivateVector vector =NewVector ();4     //Add a single element5      Public voidAdd (Object object) {6          This. Vector.add (object);7     }8     //Reduce One element9      Public voidremove (Object object) {Ten          This. Remove (object); One     } A     //returns the Iteration object -      Public voidAdd (Object object) { -         return NewConcreteiterator ( This. vector); the     } -}
View Code

 The above is the implementation of the specific general method.

Iterator mode applications

  Although I do not use this mode, but the iteration of this container is used very frequently, often programming you have seen a lot of ~ below to introduce the specific use of occasions, in fact, I can use:

    • Accesses the contents of an aggregated object without exposing its internal representation.
    • Supports multiple traversal of an aggregated object.
    • Provides a unified interface for traversing different aggregation structures (that is, support for polymorphic iterations).

Above. To know what the post-style, and listen to tell.

  

PS: This blog welcome forwarding, but please specify the blog address and author ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.gitcafe.com

<. ))) ≦

6th of the design pattern-iterator mode (Java implementation)

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.