Open In App

Design Patterns in Java – Iterator Pattern

A design pattern is proved solution for solving the specific problem/task. We need to keep in mind that design patterns are programming language independent for solving the common object-oriented design problems. In Other Words, a design pattern represents an idea, not a particular implementation. Using design patterns you can make your code more flexible, reusable, and maintainable.

Types of design patterns: There are 3 types of Design Patterns in java that are depicted more clearly in a tabular format below.



  1. Behavioral Design Pattern
  2. Creational Design Pattern
  3. Structural Design Pattern
Behavioral Creational Structural
Iterator Pattern Factory Pattern Adapter Pattern
Interpreter Pattern Abstract Factory Pattern Bridge Pattern
Mediator Pattern Singleton Pattern Composite Pattern
Memento Pattern Prototype Pattern Decorator Pattern
Observer Pattern Builder Pattern Facade Pattern
State Pattern Object Pool Flyweight Pattern
Strategy Pattern   Proxy Pattern
Template Pattern    
Visitor Pattern    

Behavioural – Iterator Pattern

Here we will be discussing Iterator Pattern with an example.



Design: Iterator Pattern

There are some pitfalls of iterator which are as follows:

Example:




// Java Program to Implement Behavioral>Iterator Pattern
 
// importing required package to
// implement Behavioral>Iterator Pattern
package com.demo.application;
 
// Importing required libraries
// Here Iterator Interface to
// use it to iterate over the elements
import java.util.Iterator;
 
 
// Class 1
 
// Car Class Implementing Iterable Interface so
// that we can implement the iterator method and
// add our own implementation
 
public class Car implements Iterable<String> {
  private String[] cars;
  private int index;
 
  // Default Constructor
  public Car() {
    cars = new String[10];
    index = 0;
  }
 
  // Method 1
  // Adding method to add Cars
  public void addCar(String car) {
    if (index == cars.length) {
      String[] largerCars = new String[cars.length + 5];
      System.arraycopy(cars, 0, largerCars, 0, cars.length);
      cars = largerCars;
      largerCars = null;
    }
    cars[index] = car;
    index++;
  }
 
  // Method 2
  // Implementing the iterator method and
  // adding your own implementation
  @Override
  public Iterator<String> iterator() {
    Iterator<String> it = new Iterator<String>() {
 
      private int currentIndex = 0;
 
      // Method 3
      // Finding whether during iteration if
      // there is next element or not
      @Override
      public boolean hasNext() {
        return currentIndex < cars.length && cars[currentIndex] != null;
      }
 
      // Method 4
      // Going to grab each car element by one by one
      // according to the index
      @Override
      public String next() {
        return cars[currentIndex++];
      }
 
      // Method 5
      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
 
    return it;
  }
 
  // Method 6
  // Main driver method
  public static void main(String[] args) {
 
    // Instantiating Car object
    Car cars = new Car();
 
    // Adding cars to the Array
    cars.addCar("Dodge");
    cars.addCar("Ferrari");
    cars.addCar("Sedan");
 
    // Creating an Iterator and pointing the cursor
    // to the index just before the first element in cars
    Iterator<String> carIterator = cars.iterator();
 
    // Checking whether the next element is available or not
    while (carIterator.hasNext()) {
      System.out.println(carIterator.next());
    }
  }
}

Conclusion:

Further Read: Java Design Patterns Tutorial


Article Tags :