Design Pattern-iterator pattern (iterator)

Source: Internet
Author: User
Iterator Mode Overview
 
Given a language, it defines a representation of its syntax and an interpreter that uses this representation to interpret sentences in the language.
 
Applicability
 
1. Access the content of an aggregate object without exposing its internal representation. 2. Supports multiple traversal of aggregate objects. 3. provides a unified interface for Traversing different aggregation structures (that is, supporting multi-state iteration ).
 
Participants
 
1. The iterator defines interfaces for accessing and traversing elements. 2. The concreteiterator implements the iterator interface. Tracks the current position of the aggregated time. 3. Aggregate aggregate defines the interface for creating the corresponding iterator object. 4. The specific aggregation Implementation of concreteaggregate creates the interface for the corresponding iterator. This operation returns an appropriate instance of concreteiterator.
 Example Public interface iterator {object next (); void first (); void last (); Boolean hasnext ();} concreteiterator public class iteratorimpl implements iterator {private list; private int index; public iteratorimpl (list) {Index = 0; this. list = List;} public void first () {Index = 0;} public void last () {Index = List. getsize ();} public object next () {object OBJ = List. get (INDEX); index ++; return OBJ;} public Boolean hasnext () {return index <list. getsize () ;}} aggregate public interface list {iterator (); object get (INT index); int getgetsize (); void add (Object OBJ );} concreteaggregate public class listimpl implements list {private object [] list; private int index; private int size; Public listimpl () {Index = 0; size = 0; list = new object [100];} public iterator () {return New iteratorimpl (this);} public object get (INT index) {return list [Index];} public int getsize () {return this. size ;}public void add (Object OBJ) {list [index ++] = OBJ; size ++ ;}} test public class test {public static void main (string [] ARGs) {list = new listimpl (); list. add ("A"); list. add ("to an item. add ("C"); // The first iteration method iterator it = List. iterator (); While (it. hasnext () {system. out. println (it. next ();} system. out. println ("===="); // the second iteration method for (INT I = 0; I <list. getsize (); I ++) {system. out. println (list. get (I) ;}} result ABC === ABC

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.