Design mode Iterator - Name article
I have been in so many years, I found a question, as if the teacher really likes some names, even the name has become a hobby of some teachers, not name a day, eat unknone, sleep, I feel like I feel like sleeping, I think Very strange, your class should be good, how can the classmate will not come to class, I don't know: "The misunderstanding is a crime!"
Ok, then we are now looking at the teacher this point process.
1. Old rules, let's define the teacher (TEACHER) interface:
Public interface teacher {
Public iterator createTerator (); // Name
}
2, the concreteteacher class is the implementation of the teacher's interface:
Public Class Concretetecher Implements Teacher {
Private object [] present = {"Zhang Sancheng", "Li Si is coming", "Wang Wu did not come"}; // Classics attendance collection
Public iterator createTeiterator () {
Return New Concreterator (this); // New Name
}
Public Object getElement (int index) {// receives attendance at the current classmate
IF (index Return present [index]; } Else { Return NULL; } } Public int getSize () { Return present.Length; // Get the size of the classmates attendance, that is, how many people do you want to know? } } 3, define the point name (Iterator) interface class: Public interface itrator { Void first (); // first Void next (); // Next Boolean isdone (); // Whether you have some name Object currentItem (); // Current students attendance } 4. The specific name (ConcreteTeiterator) is an implementation of the Iterator interface: Public class contreteiterator imports iterator { PRIVATE CONCRETECHER TEACHER; Private int index = 0; Private int size = 0; Public Concreterator (Concretetecher Teacher) { THIS.TEacher = Teacher; Size = teacher.getsize (); // Get the number of classmates INDEX = 0; } Public void first () {// first INDEX = 0; } Public void next () {// Next IF (INDEX INDEX ; } } Public boolean isdone () {// is finished Return (INDEX> = size); } Public Object CurrentItem () {// Current Classics Attendance Return teacher.getElement (Index); } } 5, write test class: public class test { Private itrator it; PRIVATE TEACHER TEACHER = New ConcreteTeacher (); Public void Operation () { It = teacher.createiterator (); // Teacher starts While (! it.isdone ()) {// If there is no finish System.out.println (it.currentItem (). TOSTRING ()); // Getting to be a case IT.next (); // Point Next } } Public static void main (string agrs []) { Test test = new test (); Test.operation (); } } 6. Description: A: Definition: The Iterator mode can sequentially access a gathering element without exposing the internal conditions of the aggregation. B: In this example, Teacher gives the interface of the Iterator object, the point name (iTerator) defines the interface required to traverse students attendance. C: The advantage of the Iterator mode is that there is a change in the ConcreteTeacher object. For example, there is no new classmate in the classmate attendance, or when you reduce your classmates, this change has no effect on the client.