Design mode (-----) iterator mode

Source: Internet
Author: User

Iterator mode (Iterator) definition

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

UML Class Diagram

Role

  iterator role (Iterator): the interface responsible for defining access and traversal elements.
  Specific iterator role (concrete Iterator): Implements the iterator interface and records the current position in the traversal.
  Container Role (Aggregate): is responsible for providing the interface that creates the specific iterator role.
  Specific container role (concrete Aggregate): Implements the interface that creates the specific iterator role, which is related to the structure of the container.

Example

Iterator

 package   Com.csdhsm.pattemdesign.iterator;  /**   * @Title: Iterator.java * @Description: Iterate abstract class *   @author  : Han * @date: June 26, 2016 PM 3:12:33  */ public  interface   Iterator { //  Determine if there is another  public  boolean   Hasnext ();  //  takes out the current element, and the cursor moves down one  public   Object Next ();}  

Concreteiterator

 PackageCom.csdhsm.pattemdesign.iterator;Importjava.util.List;/*** @Title: Concreteiterator.java * @Description: specific Iteration Implementation *@author: Han * @date: June 26, 2016 PM 3:07:14*/   Public classConcreteiteratorImplementsIterator {PrivateList<object>list; //current cursor, initially 0    Private intcursor = 0;  PublicConcreteiterator (list<object>list) {         This. List =list; } @Override Public BooleanHasnext () {return! (Cursor = =list.size ()); } @Override Publicobject Next () {Object Object=NULL; if(Hasnext ()) {//gets the current cursor corresponding element, and the cursor adds 1Object = List.get (cursor++); }                returnobject; }}

Aggregate

 PackageCom.csdhsm.pattemdesign.iterator;/*** @Title: Aggregate.java * @Description: Aggregate abstract class *@author: Han * @date: June 26, 2016 PM 3:13:05*/   Public InterfaceAggregate {//add Element     Public voidAdd (Object obj); //Delete Element     Public voidRemove (Object obj); //Get iterators     PublicIterator Iterator ();}

Concreteaggregate

 PackageCom.csdhsm.pattemdesign.iterator;Importjava.util.List;/*** @Title: Concreteaggregate.java * @Description: Specific aggregation class *@author: Han * @date: June 26, 2016 PM 3:13:22*/   Public classConcreteaggregateImplementsAggregate {PrivateList<object>list;  PublicConcreteaggregate (list<object>list) {         This. List =list; } @Override Public voidAdd (Object obj) { This. List.add (obj); } @Override Public voidRemove (Object obj) { This. List.remove (obj); } @Override PublicIterator Iterator () {return NewConcreteiterator ( This. list); }}

Client

 PackageCom.csdhsm.pattemdesign.iterator;Importjava.util.ArrayList;Importjava.util.List; Public classSolution { Public Static voidMain (string[] args) {List<Object> list =NewArraylist<>(); Aggregate Aggregate=Newconcreteaggregate (list); Aggregate.add ("Zhang San"); Aggregate.add ("John Doe"); Aggregate.add ("Wang er"); Aggregate.add (Mazi); Iterator Iterator=Aggregate.iterator ();  while(Iterator.hasnext ()) {System.out.println (Iterator.next ()); }    }}

Results

Summarize

  

The advantages of the iterator pattern are:

    • Simplified traversal method, for the object collection of traversal, or more cumbersome, for arrays or have a sequence of tables, we can still get through the cursor, but users need to understand the set is very clear premise, self-traversal objects, but for the hash table, the user traversal is more troublesome. With the introduction of the iterator method, the user is much simpler to use.
    • can provide a variety of traversal methods, such as the sequence of the table, we can provide a positive sequence traversal, reverse-traverse the two iterators, the user only need to get our implementation of a good iterator, we can easily traverse the collection.
    • Encapsulation is good, users only need to get iterators to traverse, and for the traversal algorithm is not to care.

Disadvantages of the iterator pattern:

    • For a simpler traversal (like an array or a sequence table), it's tedious to iterate with an iterator, and everyone might feel like ArrayList, and we'd rather use the for loop and get methods to iterate through the collection.

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.