Design mode iterator mode

Source: Internet
Author: User

The iterator pattern (Iterator pattern) is a very common design pattern in the Java and. Net programming environments. This pattern is used to sequentially access the elements of the collection object without needing to know the underlying representation of the collection object.

The iterator pattern belongs to the behavioral pattern.

Introduced

Intent: provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.

The main solution: different ways to traverse the entire consolidated object.

when to use: iterate through an aggregation object.

How to fix it: the responsibility to walk between elements is given to the iterator, not to the aggregation object.

Key code: Define Interface: Hasnext, next.

Application Examples: The iterator in JAVA.

Pros: 1, it supports iterating through an aggregation object in different ways. 2, the iterator simplifies the aggregation class. 3, there can be multiple traversal on the same aggregation. 4, in the iterator mode, it is convenient to add new aggregation classes and iterator classes without modifying the original code.

disadvantage: because 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.

usage Scenario: 1. Access the contents of an aggregated object without exposing its internal representation. 2, you need to provide multiple traversal methods for the aggregation object. 3. Provide a unified interface for traversing different aggregation structures.

Note: The iterator pattern separates the traversal behavior of the collection object, abstracting 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.

Realize

We will create a Iterator interface that describes the navigation method and a Container interface that returns an iterator. An entity class that implements the Container interface will be responsible for implementing the Iterator interface.

Iteratorpatterndemo, our demo class uses the entity class namesrepository to print Names stored as a collection in namesrepository .

Step 1

Create an interface.

 Public Interface Iterator {   publicboolean  hasnext ();     Public Object Next ();}
 Public Interface Container {   public  Iterator getiterator ();}
Step 2

Create an entity class that implements the Container interface. This class has an internal class Nameiteratorthat implements the Iterator interface.

 Public classNamerepositoryImplementsContainer { PublicString names[] = {"Robert", "John", "Julie", "Lora"}; @Override PublicIterator Getiterator () {return NewNameiterator (); }   Private classNameiteratorImplementsIterator {intindex; @Override Public BooleanHasnext () {if(Index <names.length) {            return true; }         return false; } @Override PublicObject Next () {if( This. Hasnext ()) {            returnnames[index++]; }         return NULL; }           }}
Step 3

Use namerepository to get the iterator and print the name.

 Public class Iteratorpatterndemo {       publicstaticvoid  main (string[] args) {       New  namerepository ();        for (Iterator iter = namesrepository.getiterator (); Iter.hasnext ();) {         = (String) Iter.next ();         System.out.println ("Name:" + name);         }}}
Step 4

Verify the output.

Name:RobertName:JohnName:JulieName:Lora

Design mode 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.