[JAVA] Iterator Pattern

Only hasNext () and next () can be operated by user By hiding the Iterator implementation class Eliminate the dependency between user and Iterator implementation class

On this page ・ Checking the structure of Iterator ・ Iterator concealment ・ Iterator Pattern and object orientation Describes about

Design Pattarm MENU

Structural confirmation of Iterator

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(){}
}

Let's actually make each class

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());}
}}

Iterator concealment

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());
}

Iterator Pattern and object orientation

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

Iterator pattern
Iterator Pattern
Design pattern ~ Iterator ~
Prototype pattern
Memento Pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator memo
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
I tried to implement the Iterator pattern
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Java design pattern
java callback pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Java pattern memo