Java design Mode _ (behavioral) _ Iterative mode __java

Source: Internet
Author: User

Quote Encyclopedia

The iterator pattern (iterator) provides a way to sequentially access a variety of elements in an aggregated object without exposing the internal representation of that object.


Applicability

Access the contents of an aggregated object without exposing its internal representation
Supports multiple traversal of aggregated objects
Provides a unified interface for traversing different aggregation structures


The roles involved in the iteration sub-pattern:
Abstract iterator (iterator) role: This abstract role defines the interface required to traverse an element.
Specific iteration sub (realiterator) Role: This role implements the iterator interface and maintains the cursor position during the iteration.
Clustered (Aggregate) role: This abstract role gives an interface to create an iterative child (iterator) object.
Concrete Aggregation (Realaggregate) role: Implements an interface to create an iterative child (iterator) object, returning an appropriate concrete iteration instance.
Client role: holds a reference to the aggregation and its iteration child objects, invokes the iterative interface of the iteration child object, and may also increase and delete aggregation elements through an iterative child operation.


Specific implementation:




Related Code

1. Abstract Clustered Class

Abstract clustered role Class public abstract
class Aggregate {
	/**
	 * Factory method, create interface for corresponding iteration child objects/
	 public
	Abstract iterator Createiterator ();
}

2, the concrete abstract aggregation class realization

The specific clustered role class public class
Realaggregate extends Aggregate {

	private object[] Objarray = null;

	/**
	 * Construction method, the specific content of incoming aggregate
	 objects
	/Public realaggregate (object[] objarray) {
		this.objarray = Objarray;
	}

	@Override Public
	iterator Createiterator () {return to
		new Realiterator (this);
	}

	/**
	 * Value method: To provide the external aggregation elements/public
	Object getelement (int index) {

		if (Index < objarray.length) { return
			Objarray[index];
		} else {return
			null;
		}
	}

	/**
	 * Value method: To provide the outside of the aggregate size *
	 * public
	int size () {return
		objarray.length;
	}
}


3, Iterative interface

Abstract iterative interface Public
interface iterator {
	/**
	 * Iterative method: Move to the first element/public
	 void a
	();

	/**
	 * Iterative method: Move to next element
	/public void Next ();

	/**
	 * Iterative method: Whether it is the last element
	 *
	/public boolean isdone ();

	/**
	 * Iterative method: Return the current element
	 *
	/Public Object CurrentItem ();
}


4, specific iterative implementation class

The specific iteration sub role class public class
Realiterator implements iterator {

	//holds the specific aggregate object being iterated to the
	private realaggregate agg;
	Internal index, which records the current iteration to the index position
	private int index = 0;
	Record the size of the current clustered object
	private int size = 0;

	Public Realiterator (Realaggregate agg) {
		This.agg = agg;
		This.size = Agg.size ();
		index = 0;
	}

	/**
	 * Iterative method: Return the current element
	 *
	/@Override public
	Object CurrentItem () {returns
		agg.getelement (index);

	/**
	 * Iterative method: Move to the first element
	/@Override public void Primary
	() {
		index = 0;
	}

	/**
	 * Iterative method: Whether it is the last element
	 *
	/@Override public
	Boolean Isdone () {return
		(index >= size);
	}

	/**
	 * Iterative method: Move to next element
	/@Override public
	Void Next () {
		if (Index < size) {
			index ++;
		}
	}

}


5. Client-side test

public class Client {public

	static void operation () {
		object[] Objarray = {"One", "two", "Three", "Four", "Five" , "Six"};
		Create an Aggregation object
		Aggregate Aggregate = new Realaggregate (objarray);
		Loop output the value in the aggregate object
		iterator it = Aggregate.createiterator ();
		while (!it.isdone ()) {
			System.out.println (It.currentitem ());
			It.next ();
		}

	public static void Main (string[] args) {
		operation ();
	}
}

The iterative mode is implemented through the simple code above, while the loop iteration outputs the contents of the array.


Iteration Mode effect:

It supports traversing an aggregation in a different way. Iterators simplify the interface of aggregation. There can be multiple traversal on the same aggregation.
Advantages of iterative sub-schemas

1 The iterative sub mode simplifies the aggregation interface. The iterator has a traversal interface so that the aggregated interface does not have to have a traversal interface.

2 Each Aggregation object can have one or more iterative child objects, and each iteration state can be independent of each other. Therefore, a clustered object can have several iterations in progress at the same time.

3 Because the traversal algorithm is encapsulated in the iteration sub role, the iterative algorithm can be independent of the aggregation role change.




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.