This article describes the access person list operation based on the iterator mode of Java. Share it for your reference, as follows:
A pattern definition
Iterator mode provides a schema to access individual elements in a collection object in sequence without exposing its internal representation.
Examples of the second mode
1 Pattern Analysis
We borrow the case of visitor list to illustrate this pattern.
2 Iterator mode static class diagram
3 Code Examples
3.1 Personnel Information Interface – IPerson
package com.demo.person;/** * Personnel information* * @author * */public interface IPerson{ /** * Obtain personnel information* * @return */ public String getPersonInfo();}3.2 Personnel information implementation - Person
package com.demo.person;/** * Person 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; /** * Constructor method setting attribute content* * @param name * @param age * @param sex */ public Person(String name, int age, int sex) { this.name = name; this.age = age; this.sex = sex; } /** * Obtaining personnel information* * @return */ public String getPersonInfo() { return "Name:" + this.name + " - Age:" + this.age + " - Gender:" + (this.sex == 1 ? "Male" : (this.sex == 0 ? "Female" : "")); }}3.3 Personnel collection interface - IPersonList
package com.demo.person;import com.demo.iterator.Iterator;/** * Personnel interface* * @author * */public interface IPersonList { /** * Get internal storage of personnel information content* * @return */ public IPerson[] getPersonList(); /** * Iterator* * @return */ public Iterator iterator();}3.4 PersonList
package com.demo.person;import java.util.Random;import com.demo.iterator.ArrPersonIterator;import com.demo.iterator.Iterator;/** * Personlist information* * @author * */public class PersonList implements IPersonList { // Store user information list private final IPerson[] list = new IPerson[10]; // Construct method initializes personnel information public PersonList() { Random random = new Random(); // Create personnel information for (int i = 0; i < 10; i++) { IPerson person = new Person("Person" + i, random.nextInt(30), random .nextInt(2)); list[i] = person; // this.list.add(person); } } /** * Get internal storage personnel information content* * @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 whether the next node contains 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 attributes store personnel list object information private final IPerson[] personList; // The initial value of storage location information is -1 private int index = -1; /** * Constructor method passes the personnel list object in * * @param personList */ public ArrPersonIterator(IPerson[] personList) { this.personList = personList; } // determine whether the next node is contained 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;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("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- personList.iterator(); // Loop iterator traverses each element and outputs personnel information while (iterator.hasNext()) { // Obtains the personnel object instance IPerson person = (IPerson) iterator.next(); if (person != null) { // Output personnel information System.out.println(person.getPersonInfo()); } } System.out.println("----Use the iterator to output personnel information end..."); }}4 Running results
----Use the iterator to output personnel information start...
Name: Personnel 0 - Age: 28 - Gender: Female Name: Personnel 1 - Age: 25 - Gender: Female Name: Personnel 2 - Age: 23 - Gender: Female Name: Personnel 3 - Age: 18 - Gender: Female Name: Personnel 4 - Age: 27 - Gender: Female Name: Personnel 5 - Age: 28 - Gender: Male Name: Personnel 6 - Age: 25 - Gender: Female Name: Personnel 7 - Age: 23 - Gender: Female Name: Personnel 8 - Age: 16 - Gender: Male Name: Personnel 9 - Age: 12 - Gender: Female
----Use the iterator to output the personnel information end...
Three principles of design of this pattern
1 "Open and Close" Principle
2 Single Responsibility Principle
Four usage occasions
1Access the contents of a collection object without exposing its internal representation.
2 Supports multiple traversal methods of collection objects.
3 Provides a unified interface for traversing different collection object structures.
Five iterator mode static class diagram
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.