Big talk Design Pattern _ iterator Pattern

Source: Internet
Author: User

Take bus ticket conductor as an example.

I. Self-implemented iterator

Package COM. WZS. design; import Java. util. arraylist; import Java. util. list;/*** big talk design mode -- page202 iterator mode ** @ author administrator **/public class iteratorpattern {public static void main (string [] ARGs) {concreteaggregate A = new concreteaggregate ();. set ("laruence");. set ("Pickles");. set ("Luggage");. set ("foreigner");. set ("Bus internal staff");. set ("thief"); iterator = new concreteiterator (a); While (! Iterator. iddone () {system. out. println (iterator. currentitem () + "\ t buy a ticket. "); iterator. next () ;}}/ ** iterator abstract class */abstract class iterator {// The first public abstract object first (); // next public abstract object next (); // public Abstract Boolean iddone () at the end; // The current object public abstract object currentitem ();} /** specific iterator */class concreteiterator extends iterator {private concreteaggregate aggregate; private int Current = 0; Public concreteiterator (concreteaggregate aggregate) {This. aggregate = aggregate;} @ overridepublic object first () {return aggregate. get (0) ;}@ overridepublic object next () {object ret = NULL; current ++; If (current <aggregate. count () {ret = aggregate. get (current);} return ret;} @ overridepublic object currentitem () {return aggregate. get (current) ;}@ overridepublic Boolean iddone () {return cur Rent> = aggregate. Count ()? True: false;}/** aggregate aggregation abstract class */abstract class aggregate {public abstract iterator createiterator ();} /** concreteaggregate specific aggregation class */class concreteaggregate extends aggregate {private list <Object> items = new arraylist <Object> (); @ overridepublic iterator createiterator () {return New concreteiterator (this);} public int count () {return items. size ();} public object get (INT index) {return items. get (INDEX);} public void set (object Value) {items. add (value );}}

Ii. iterator in Java

Package COM. WZS. design; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Java. util. random;/** Java iterator */public class javaiterator {public static void main (string [] ARGs) {list <integer> List = new arraylist <integer> (); random random = new random (); For (INT I = 0; I <10; I ++) {list. add (random. nextint (20);} system. out. println ("array element:" + list); system. out. println ("Iteration Result:"); iterator <integer> iterator = List. iterator (); While (iterator. hasnext () {system. out. print (iterator. next () + "");}}}

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.