Design Pattern iterator mode

Source: Internet
Author: User

iterator pattern definition: It provides a way to access individual elements in a container object without exposing the internal details of the object.

(Iterators are serviced for containers.) All types that can hold an object can be called containers. For example: Collection collection type, map type. Since the collection now provides iterators, this pattern is rarely applied at this time)

Iterators have the following roles:

1, iterator abstract iterator. There are three basic methods: Hasnext (), Next (), remove ().

2, Concreteiterator specific iterators.

3, aggregate abstract container.

4, concreteaggregate specific containers.

The class diagram is as follows:


The implementation code is as follows:

Abstract iterator:

Package Com.designpatterns.iterator;import java.util.iterator;/** * @author WSYW126 * @version May 5, 2016 6:42:28 * * Public interface Iprojectiterator extends iterator<object> {}

Iterator implementation class:

Package Com.designpatterns.iterator;import java.util.arraylist;/** * @author WSYW126 * @version May 5, 2016 6:43:37 * * public class Projectiterator implements Iprojectiterator {private arraylist<iproject> rs = new arraylist<> () ;p rivate int currentitem = 0;public Projectiterator (arraylist<iproject> rs) {this.rs = rs;} @Overridepublic Boolean Hasnext () {Boolean b = false;if (This.currentitem < This.rs.size ()) {b = true;} return b;} @Overridepublic IProject Next () {return this.rs.get (currentitem++);} @Overridepublic void Remove () {}}

Abstract Container:

Package com.designpatterns.iterator;/** * @author WSYW126 * @version May 5, 2016 PM 6:45:09 */public interface IProject {Publ IC void Add (string name, int num, int cost);p ublic String getprojectinfo ();p ublic Iprojectiterator iterator ();}

Container Implementation class:

Package Com.designpatterns.iterator;import java.util.arraylist;/** * @author WSYW126 * @version May 5, 2016 6:49:21 * * public class Project implements IProject {private arraylist<iproject> rs = new arraylist<> ();p rivate String na me = "";p rivate int num = 0;private int cost = 0;public Project () {}private project (String name, int num, int. cost) {this. Name = Name;this.num = Num;this.cost = Cost;} @Overridepublic void Add (String name, int num, int cost) {This.rs.add (new Project (name, num, cost));} @Overridepublic string Getprojectinfo () {String inf = ""; inf = inf + "Project name is:" + This.name;inf = inf + "\ T Project number:" + this.nu M;inf = inf + "\ T project cost:" + This.cost;return inf;} @Overridepublic Iprojectiterator iterator () {return new projectiterator (this.rs);}

Test class:

Package com.designpatterns.iterator;/** * @author WSYW126 * @version May 6, 2016 PM 6:06:13 */public class Boss {public static void Main (string[] args) {IProject project = new Project ();p Roject.add ("StarCraft", Max.);p Roject.add ("Superhero", 100, 100000) ; for (int i = 0; i < i++) {Project.add ("+ i +" items ", I * 5, I * 10000);} Iprojectiterator projectiterator = Project.iterator (); while (Projectiterator.hasnext ()) {IProject p = (IProject) Projectiterator.next (); System.out.println (P.getprojectinfo ());}}}

This is the iterator pattern.

References :
The Zen of design pattern

Remark :
Reprint please indicate the source
http://blog.csdn.net/wsyw126/article/details/51333750
by WSYW126

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