Iterator pattern - access to the list of people

a schema definition

The iterator pattern provides a pattern to sequentially access the elements of a collection object without exposing its internal representation.

 

Two-mode example

1 Pattern Analysis

We use the case of visiting a list of people to illustrate this pattern.

2 Iterator pattern static class diagram



 

3 Code Examples

3.1 Personnel Information Interface - IPerson

package com.demo.person;

/**
 * Personnel information
 *
 * @author
 *
 */
public interface IPerson
{
	/**
	 * Get personnel information
	 *
	 * @return
	 */
	public String getPersonInfo();

}

 

3.2 Personnel information realization - Person

package com.demo.person;

/**
 * Personnel specific implementation class
 *
 * @author
 *
 */
public class Person implements IPerson
{
	// Name
	private String name;
	// age
	private int age;
	// gender (1: male 0: female)
	private int sex;

	/**
	 * The constructor sets the property content
	 *
	 * @param name
	 * @param age
	 * @paramsex
	 */
	public Person(String name, int age, int sex)
	{
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	/**
	 * Get personnel information
	 *
	 * @return
	 */
	public String getPersonInfo()
	{
		return "name:" + this.name + " - age:" + this.age + " - gender:" + (this.sex == 1 ? "male" : (this.sex == 0 ? "female" : ""));
	}

}

 

3.3 Person Collection Interface - IPersonList

package com.demo.person;

import com.demo.iterator.Iterator;

/**
 * Personnel interface
 *
 * @author
 *
 */
public interface IPersonList {
	/**
	 * Get the content of internal storage personnel information
	 *
	 * @return
	 */

	public IPerson[] getPersonList();

	/**
	 * iterator
	 *
	 * @return
	 */
	public Iterator iterator();
}

 

3.4 Person Collection Implementation - PersonList

package com.demo.person;

import java.util.Random;

import com.demo.iterator.ArrPersonIterator;
import com.demo.iterator.Iterator;

/**
 * Personnel list information
 *
 * @author
 *
 */
public class PersonList implements IPersonList {
	// store user information list
	private final IPerson[] list = new IPerson[10];

	// The constructor initializes the personnel information
	public PersonList() {
		Random random = new Random();
		// create person information
		for (int i = 0; i < 10; i++) {
			IPerson person = new Person("人员" + i, random.nextInt(30), random
					.nextInt(2));
			list[i] = person;
			// this.list.add(person);
		}
	}

	/**
	 * Get the content of internal storage personnel information
	 *
	 * @return
	 */

	public IPerson[] getPersonList() {
		return list;
	}

	/**
	 * iterator
	 *
	 * @return
	 */
	public Iterator iterator() {
		return new ArrPersonIterator(this.list);
	}
}

 

3.5 Iterator Interface - Iterator

package com.demo.iterator;

/**
 * Iterator interface
 *
 * @author
 *
 */
public interface Iterator {

	// Determine if it contains the next node
	public boolean hasNext();

	// get the next node object
	public Object next();

	// delete the object
	public Object remove();
}

 

3.6 Iterator Implementation - ArrPersonIterator

package com.demo.iterator;

import com.demo.person.IPerson;

/**
 * Array iterator implementation
 *
 * @author
 *
 */
public class ArrPersonIterator implements Iterator {
	// Private property to store person list object information
	private final IPerson [] personList;
	// The initial value of the storage location information is -1
	private int index = -1;

	/**
	 * The constructor passes in the person list object
	 *
	 * @param personList
	 */
	public ArrPersonIterator (IPerson [] personList) {
		this.personList = personList;
	}

	// Determine if it contains the next node
	public boolean hasNext() {
		return (this.personList == null ? false
				: (index < this.personList.length - 1));
	}

	// get the next node object
	public Object next() {
		if (this.personList != null && (index < this.personList.length - 1)) {
			// Get the personnel information in the personnel list object
			return this.personList[++index];
		}
		return null;
	}

	// delete the object
	public Object remove() {
		if (this.personList != null) {
			IPerson person = this.personList[index];
			this.personList[index] = null;
			return person;
		}

		return null;
	}

}

 

3.7 Let the iterator traverse the collection object - Client

package com.demo;

import com.demo.iterator.Iterator;
import com.demo.person.IPerson;
import com.demo.person.IPersonList;
import com.demo.person.PersonList;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// create a person list object
		IPersonList personList = new PersonList();
		System.out.println("----Use iterator to output personnel information start......");
		// generate iterator
		Iterator iterator = personList.iterator();
		// The loop iterator traverses each element and outputs the personnel information
		while (iterator.hasNext()) {
			// get the person object instance
			IPerson person = (IPerson) iterator.next ();
			if (person != null) {
				// output personnel information
				System.out.println(person.getPersonInfo());
			}
		}
		System.out.println("----Use iterator to output personnel information end...");

	}
}

 

4 Running results

----Use iterator to output personnel information start......

Name: Person 0 - Age: 28 - Gender: Female

Name: Person 1 - Age: 25 - Gender: Female

Name: Person 2 - Age: 23 - Gender: Female

Name: Person 3 - Age: 18 - Gender: Female

Name: Person 4 - Age: 27 - Gender: Female

Name: Person 5 - Age: 28 - Gender: Male

Name: Person 6 - Age: 25 - Gender: Female

Name: Person 7 - Age: 23 - Gender: Female

Name: Person 8 - Age: 16 - Gender: Male

Name: Person 9 - Age: 12 - Gender: Female

----Use iterator to output personnel information end......

 

Three principles of pattern design

1 "Open and close" principle

2 Single Responsibility Principle

 

Four use cases

1 Access the contents of a collection object without exposing its internal representation.

2 Supports multiple traversal methods for collection objects.

3 Provide a unified interface for traversing different collection object structures.

 

Five iterator pattern static class diagram



 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613593&siteId=291194637