[Design mode] -- iteration mode Iterator

Source: Internet
Author: User

[Mode overview] ---------- by xingoo

Pattern intent

Provides a method to traverse elements in a set sequentially without exposing the internal representation of the object.

Application scenarios

1. Access an aggregated object without exposing the internal representation of the object

2. Multiple traversal of aggregate objects

3 provides a unified interface for traversing different objects.

Mode structure

Iterator defines the access interface

/*** Abstract iteration, which has the judgment end and the next one. Obtain the current element and other functions * @ author xingoo **/interface Iterator {void first (); void next (); boolean isDone (); Object currentItem ();}

 

The specific iterator of ConcreteIterator to track the elements in the aggregation.

/*** Specific iteration class ** @ author xingoo **/class ConcreteIterator implements Iterator {private ConreteAggregate break; private int index = 0; private int size = 0; public ConcreteIterator (ConreteAggregate iterator) {this. bytes = bytes; size = bytes. size (); index = 0;} public void first () {index = 0;} public void next () {if (index <size) {index ++ ;}} public boolean isDone () {return (index> = size);} public Object currentItem () {return failed. getElement (index );}}

 

Aggregate provides an aggregation interface

/*** Aggregate class * @ author xingoo **/abstract class Aggregate {public Iterator createIterator () {return null ;}}

 

Specific aggregation of ConcreteAggregate

/*** Specific Aggregate Object with size, create iteration sub-function * @ author xingoo **/class ConreteAggregate extends Aggregate {private Object [] obj = {"test1 ", "test2", "test3", "test4"}; public Iterator createIterator () {return new ConcreteIterator (this);} public Object getElement (int index) {if (index <obj. length) {return obj [index];} else {return null ;}} public int size () {return obj. length ;}}

All code

1 package com. xingoo. iterator; 2/** 3 * aggregation class 4 * @ author xingoo 5*6 */7 abstract class Aggregate {8 public Iterator createIterator () {9 return null; 10} 11} 12/** 13 * abstract iteration, with the end of judgment and the next, obtain the current element and other functions 14 * @ author xingoo15 * 16 */17 interface Iterator {18 void first (); 19 void next (); 20 boolean isDone (); 21 Object currentItem (); 22} 23/** 24 * specific aggregate Object, with size, create iteration sub-function 25 * @ author xingoo26 * 27 */28 class Conrete Aggregate extends Aggregate {29 private Object [] obj = {"test1", "test2", "test3", "test4"}; 30 public Iterator createIterator () {31 return new ConcreteIterator (this); 32} 33 public Object getElement (int index) {34 if (index <obj. length) {35 return obj [index]; 36} else {37 return null; 38} 39} 40 public int size () {41 return obj. length; 42} 43} 44/** 45 * specific iteration class 46 * @ author xingoo47 * 48 */49 class concretei.pdf Tor implements Iterator {50 private ConreteAggregate instances; 51 private int index = 0; 52 private int size = 0; 53 54 public ConcreteIterator (ConreteAggregate instances) {55 this. bytes = bytes; 56 size = bytes. size (); 57 index = 0; 58} 59 60 public void first () {61 index = 0; 62} 63 64 public void next () {65 if (index <size) {66 index ++; 67} 68} 69 70 public boolean isDone () {71 return (index> = size ); 72} 73 74 public Obj Ect currentItem () {75 return failed. getElement (index); 76} 77 78} 79/** 80 * Client usage 81 * @ author xingoo82 * 83 */84 public class Client {85 private Iterator it; 86 private Aggregate operation = new ConreteAggregate (); 87 public void operation () {88 it = operation. createIterator (); 89 while (! It. isDone () {90 System. out. println (it. currentItem (). toString (); 91 it. next (); 92} 93} 94 public static void main (String [] args) {95 Client client = new Client (); 96 client. operation (); 97} 98}
View Code

Running result

Test1test2test3test4

 

[Design mode] -- iteration mode Iterator

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.