Iterator pattern of design pattern 14

background

First, let's look at a piece of code:

List<Member> memberList = listMember();
Iterator<Member> iterator = memberList.iterator();
while (iterator.hasNext()) {
    Member next = iterator.next();
    ...
}

The above code uses an iterator to traverse the List. Let's take a look at the following traversal method:

for (Member member : memberList) {
    ...
}

The above traversal is a direct traversal.

Direct traversal is the traversal process in which the collection directly participates. The traversal method here is too coupled with the collection object. If we manipulate the collection in direct traversal, an operation exception occurs.

Using iterator traversal, we separate the collection object and its traversal behavior. When the client traverses the collection, it is operated through the iterator. For the client, the internal details of the collection are hidden. With iterator traversal, we will have no problem operating the collection while traversing the collection.

In fact, iterator traversal uses the iterator pattern.

What is the iterator pattern

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. (It provides a way to access the elements of a container object without exposing the internal details of the object.)

The iterator mode mainly consists of the following 4 elements:

  • Abstract aggregation (Aggregate) role: defines the interface for storing, adding, and deleting aggregate objects, and creating iterator objects.

  • Concrete Aggregate (ConcreteAggregate) role: implements an abstract aggregation class and returns an instance of a concrete iterator.

  • Abstract Iterator role: defines the interface for accessing and traversing aggregate elements, usually including methods such as hasNext(), first(), next(), etc.

  • Concrete iterator (Concretelterator) role: implement the methods defined in the abstract iterator interface, complete the traversal of the aggregate object, and record the current position of the traversal.

The structure diagram of the iterator mode is as follows:

Code

Aggregate

public interface Aggregate {
    void add(Object obj);
    void remove(Object obj);
    Iterator getIterator();
}

Iterator

public interface Iterator {
    Object first();
    Object next();
    boolean hasNext();
}

ConcreteAggregate

ConcreteIterator

Test code:

Test Results:

聚合的内容有:微信号:Lvshen_9 头条号:Lvshen的技术小屋 CSDN:Lvshen的技术小屋 
First:微信号:Lvshen_9

Thoughts on the iterator pattern

In Java, collections provide iterator classes. If we want to write a collection-like aggregation class ourselves, we can use the iterator class. Using the iterator pattern, the aggregated object may be accessed without exposing internal details, which protects the security of the aggregated object's data.

In fact, in Java development, you may not be able to use the iterator mode, because in the Java class library, all libraries that need to use the iterator mode already provide an iterator class.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

 

Guess you like

Origin blog.csdn.net/wujialv/article/details/109441778