Iterator pattern of Java design pattern

Iterator pattern of Java design pattern

In software development, the iterator pattern is a behavioral design pattern that allows us to access the elements of an aggregate object without exposing the internal structure of the aggregate object. This pattern is a behavioral pattern because it can change the behavior of an object at runtime.

In this article, we will introduce the basic principles of the iterator pattern and demonstrate how to use iterator pattern through a simple case.

Fundamentals of the Iterator Pattern

The Iterator pattern provides a way to iterate over an aggregate object without exposing the internal structure of the aggregate object. This pattern traverses the aggregate object by defining a common interface, rather than exposing the implementation details of the aggregate object.

In the iterator mode, we first define an abstract iterator interface, which contains some methods, such as next(), hasNext(), etc., for traversing aggregated objects. Then, we define an abstract aggregate object interface, which includes a method createIterator() to create an iterator object.

The specific iterator class and aggregate object class implement the corresponding interface and provide implementation details.

When using the iterator pattern, we first create an aggregate object and use the createIterator() method to create an iterator object. Then, we use the iterator object to traverse the elements of the aggregate object without knowing the internal structure of the aggregate object.

Iterator pattern case

Suppose we are developing a store sales management system, we need to implement an order list to display all order information. We can implement a list of orders using the iterator pattern as follows:

First, we create an order class named Order, which contains some attributes, such as order number, customer name, order amount, and so on.

public class Order {
    
    
    private int orderId;
    private String customerName;
    private double orderAmount;

    public Order(int orderId, String customerName, double orderAmount) {
    
    
        this.orderId = orderId;
        this.customerName = customerName;
        this.orderAmount = orderAmount;
    }

    public int getOrderId() {
    
    
        return orderId;
    }

    public String getCustomerName() {
    
    
        return customerName;
    }

    public double getOrderAmount() {
    
    
        return orderAmount;
    }

    @Override
    public String toString() {
    
    
        return "Order{" +
                "orderId=" + orderId +
                ", customerName='" + customerName + '\'' +
                ", orderAmount=" + orderAmount +
                '}';
    }
}

Then, we create an order list class named OrderList, which implements the aggregate object interface in the iterator pattern. In the OrderList class, we include a list of type Order and implement the createIterator() method to create an iterator object

public class OrderList implements Iterable<Order> {
    
    
    private List<Order> orders;

    public OrderList() {
    
    
        orders = new ArrayList<>();
    }

    public void addOrder(Order order) {
    
    
        orders.add(order);
    }

    public void removeOrder(Order order) {
    
    
        orders.remove(order);
    }

    @Override
    public Iterator<Order> iterator() {
    
    
        return new OrderIterator();
    }

    private class OrderIterator implements Iterator<Order> {
    
    
        private int currentIndex = 0;

        @Override
        public boolean hasNext() {
    
    
            return currentIndex < orders.size();
        }

        @Override
        public Order next() {
    
    
            return orders.get(currentIndex++);
        }
    }
}

In the OrderList class, we implemented the Iterable interface and returned an iterator object of type OrderIterator in the iterator() method. In the OrderIterator class, we include a current index currentIndex and implement the hasNext() and next() methods for traversing the elements in the order list.

Now, we can create an OrderList object and add some orders using the addOrder() method. We can then iterate over the elements in the order list using the iterator object of the OrderList object as follows:

OrderList orderList = new OrderList();
orderList.addOrder(new Order(1, "Alice", 100.0));
orderList.addOrder(new Order(2, "Bob", 200.0));
orderList.addOrder(new Order(3, "Charlie", 300.0));

for (Order order : orderList) {
    
    
    System.out.println(order);
}

In this example, we first created an OrderList object and added three orders using the addOrder() method. Then, we use a for loop and an iterator object to iterate through the elements in the orders list and output information about each order.

By using the iterator pattern, we can easily traverse the elements in the aggregate object without knowing the internal structure of the aggregate object. This improves code maintainability and scalability.

Summarize

The iterator pattern is a commonly used behavioral design pattern that allows us to traverse the elements in an aggregate object without exposing the internal structure of the aggregate object. In this article, we introduced the basic principles of the iterator pattern and demonstrated how to use iterator pattern through a simple case. By implementing the iterator interface to traverse the elements in the aggregate object, we can improve the maintainability and scalability of the code.

Guess you like

Origin blog.csdn.net/Susan003/article/details/130112427