The understanding and realization of the iterator pattern of GOF23 design pattern

Source: Internet
Author: User


Iterator mode
Scene:
Provides a way to traverse an aggregated object. Also known as  cursors cursor mode
Aggregated objects: Storing data
Iterator mode: Traversing data
Aggregate abstract class Aggregate
Iterator An iterative abstract class used to define the starting object, get the next object, determine whether to the end, the current object, and other abstract methods, unified interface
Specific aggregation classes: inheriting Aggregate
Specific iterator classes, inheriting Iterator, implementing methods such as Start, next, End, current object, and so on.
Usage scenarios:
Forward iterators
Back iterators
List/set built -in iterators
/** * Custom Abstract iterator interface */package Com.bjsxt.cn.iterator;
Public interface Myiterator {public void First ();//the cursor refers to the next element public void last ();//will have a table pointing to the final element public void next (); public boolean hasnext ();//Determine if there is a second element public boolean isFirst (); public boolean islast (); public Object CurrentObj ();//Get current object}
Package com.bjsxt.cn.iterator;
[email protected] * */public class Concretemyaggregate {private list<object> List = new Arraylist<objec  T> ();  public void AddObject (Object obj) {list.add (obj);}  public object Removeobj (object obj) {return list.remove (obj);}  Get iterator public myiterator Createiterator () {return new concreteiterator (); }//using an internal class definition iterator, you can directly use the properties of the external class private class Concreteiterator implements Myiterator {
The private int cursor;//defines where the cursor is used to record the Traverse @Override public void First () {cursor = 0; }
@Override public void Last () {cursor = List.size ()-1; }
@Override public boolean Hasnext () {if (Cursor < list.size ()) {return true;  } return false; }
@Override public boolean IsFirst () {return (cursor = = 0)? true:false; }
@Override public boolean islast () {return (cursor = = List.size ()-1)? True:false; }
@Override public Object CurrentObj () {if (Cursor < list.size ()) {return list.get (cursor);  } return null; }
@Override public void Next () {if (Cursor < list.size ()) {cursor++; }     }   }}
/** * Test class * April 11, 2015 21:15:43 */package com.bjsxt.cn.iterator;
public class Client {public static void main (string[] args) {concretemyaggregate CMA = new Concretemyaggregate ();  Cma.addobject ("AA");  Cma.addobject ("BB");  Cma.addobject (New Student ("Jay Chou", 10000));    Cma.addobject (1);    Myiterator iterator = Cma.createiterator (); while (Iterator.hasnext ()) {System.out.println (iterator.   CurrentObj ());  Iterator.next (); } }}
Class Student {private String name; private int id; public Student (String name, int id) {super ();  THIS.name = name; This.id = ID; } public Student () {}
Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
public int getId () {return ID;}
public void setId (int id) {this.id = ID;} @Override public String toString () {return name + "" + "ID" + ID;}}
/* * AABB Jay ID is 100001
*  *  */

The understanding and realization of the iterator pattern of GOF23 design pattern

Related Article

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.