Introduction to the iterative mode of Java design pattern (iterator mode) _java

Source: Internet
Author: User
Tags int size

After so many years of study, I found a problem, as if teachers are very fond of roll call, even the roll call has become some of the teacher's hobby, a day does not call, on the food does not smell, sleep is not good, I feel very strange, your class if you speak well, how can students do not come to the lectures, but: "Fraught, is a crime!" "Ah.

Okay, so let's see the teacher now. How does this roll-call process come true:
1, the old rules, we first define the teacher (Teacher) interface class:

Copy Code code as follows:

Public interface Teacher {
Public iterator createiterator (); Named
}

2, the specific teacher(Concreteteacher) class is the implementation of the teacher (Teacher) interface:
Copy Code code as follows:

public class Concreteteacher implements teacher{
Private object[] present = {"John came", "Dick Came", "Harry did not Come"}; Student Attendance Collection
Public iterator Createiterator () {
Return to New Concreteiterator (this); The new Roll Call
}
Public Object getelement (int index) {//Get current student's attendance status
if (index<present.length) {
return Present[index];
}
else{
return null;
}
}
public int GetSize () {
return present.length; Get the size of the student attendance set, which means you know how many people are in the class.
}
}

3, the definition of roll(iterator) interface class:
Copy Code code as follows:

Public interface Iterator {
void A (); First one
void Next (); Next
Boolean Isdone (); is the roll-call completed
Object CurrentItem (); Attendance of current classmates
}

4, the specific roll callThe (Concreteiterator) class is an implementation of the naming (iterator) interface:
Copy Code code as follows:

public class Concreteiterator implements iterator{
Private Concreteteacher teacher;
private int index = 0;
private int size = 0;
Public Concreteiterator (Concreteteacher teacher) {
This.teacher = teacher;
Size = Teacher.getsize (); Get the number of classmates
index = 0;
}
public void Primary () {//First
index = 0;
}
public void Next () {//Next
if (index<size) {
index++;
}
}
public Boolean Isdone () {//whether the roll-call has been completed
return (index>=size);
}
Public Object CurrentItem () {//Current student Attendance
Return teacher.getelement (index);
}
}

5, Write test class :

Copy Code code as follows:

public class Test {
Private iterator it;
Private Teacher Teacher = new Concreteteacher ();
public void operation () {
it = Teacher.createiterator (); The teacher started the roll call.
while (!it.isdone ()) {//If not finished
System.out.println (It.currentitem (). toString ()); Get the point of being a classmate
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 elements of a cluster without exposing the aggregated internal situation.
B: In this case, the teacher (Teacher) gives the interface to create a naming (iterator) object, and the naming (iterator) defines the interface needed to traverse the student's attendance.
The advantage of the C:iterator model is that when there is a change in the (Concreteteacher) object, such as having a new classmate in the class attendance collection, or reducing the class, this change has no effect on the client.

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.