On this page ・ Checking the structure of Iterator ・ Iterator concealment ・ Iterator Pattern and object orientation Describes about
Check with the following class configuration
class | Explanation |
---|---|
sam.class | Array and Iterator()have |
Itarator.class | sam.Call an array element of class |
user(Main.class) | sam.use class |
The basic structure of the sam and Iterator classes looks like this:
sam.class
class sam{
int[] i= {0,1,2};
Iterator iterate(){}
}
Iterator.class
class Iterator{
boolean hasNext(){}
int next(){}
}
sam.class
class sam{
int[] i={0,1,2};
Iterator iterate(){ //Returns Iterator
return new Iterator(this.i); // Iterator.Pass an array of sam as an argument to class
}
}
Iterator.class
class Iterator{
int[] i; // sam.Stores an array of classes A
int index=0; //A variable that counts the elements of an array B
Iterator(int[] i){this.i=i;} // sam.Take an array from class and store it in A
boolean hasNext(){
boolean b=false;
if(index<i.length){b=true;} //Compare the number of elements in the array with the value of B to determine if the next element is present:
else{return b;}
return b;
}
int next(){
int ans = i[index]; //Sam the current element.Return to class
index++;
return ans;
}
}
user(Main.class)
public static void main(String[] args){
sam s1 = new sam();
Iterator it = s1.iterate();
while(it.hasNext()){
System.out.println((int) it.next());}
}}
Check with the following class configuration
class | Explanation |
---|---|
Interface.interface | user(Main.class)Is Interface and use.via class, sam.Use the function of class |
use.class | sam.Issue an instance of class |
sam.class | Implement functions used by user (array and Iterator function) |
Iterator.interface | user and samIterator.Mediate class |
samIterator | sam.Call an array element of class |
The basic class structure is as follows
Interface.interface
interface Interface{
Iterator iterate();
}
use.class
class use {
Interface newInstance(){}
}
sam.class
class sam implements Interface{
int[] i={0,1,2};
Iterator iterate(){}
}
[Iterator side]
Iterator.interface
interface Iterator{
boolean hasNext();
int next();
}
samIterator.class
class samIterator implements Iterator{
samIterator(){}
public boolean hasNext(){}
public int next(){}
}
Let's make each class
Interface.interface
interface Interface{
Iterator iterate();
}
use.class
class use {
public static Interface newInstance(){
return new sam();}
}
sam.class
class sam implements Interface{
private int[] i={0,1,2};
public Iterator iterate(){
return new samIterator(this.i);
}
}
Iterator.interface
interface Iterator{
boolean hasNext();
int next();
}
samIterator.class
class samIterator implements Iterator{
private int i[];
private int index=0;
samIterator(int[] i){
this.i=i;
}
public boolean hasNext(){
if(index<i.length){return true;}
else{return false;}
}
public int next(){
int ans = i[index];
index++;
return ans;}
}
user(Main.class)
public static void main(String[] args){
Interface face = use.newInstance();
Iterator it = face.iterate();
while(it.hasNext()){
System.out.println(it.next());
}
From user, just use use.class and two interfaces Each implementation class is Unvisible, which eliminates the dependency
The developer can change the array contents of sam.class Even if you change to branch processing to use Map depending on the conditions Nothing needs to be changed on the user side as long as Iterator works
Recommended Posts