23 design patterns (14) Java iterator pattern

23 design patterns Part 14: Java iterator pattern

Definition: provides a way to access each element in a container object without exposing the internal details of the object.

Type: behavior class pattern

Class diagram:

If you want to ask about the most used mode in Java, the answer is not the singleton mode, nor the factory mode, nor the policy mode, but the iterator mode. Let's take a look at a piece of code first:

The function of this method is to print a string set circularly, in which the iterator mode is used. The iterator mode has been completely implemented in Java language, and iterator means iterator in Chinese. When it comes to iterators, first of all, they are related to collections. Collections are also called aggregates and containers. We can regard a collection as a container that can contain objects. For example, lists, sets, maps, and even arrays can be called collections, and the function of iterators is to traverse the objects in the container one by one.

Structure of iterator pattern

Abstract container: it is generally an interface that provides an iterator () method, such as the collection interface, list interface, set interface, etc. in Java. Concrete container: it is the concrete implementation class of the abstract container, such as ArrayList for the ordered list of the list interface, linklist for the linked list of the list interface, HashSet for the Hash list of the set interface, etc. Abstract iterator: defines the methods required to traverse elements. Generally speaking, there are three methods: get the method of the first element first (), get the method of the next element next (), judge whether to traverse the end method isdone () (or hasnext ()), and remove the method of the current object remove (). Iterator implementation: implement the methods defined in the iterator interface, Complete the iteration of the set.

code implementation

In the above code, aggregate is a container class interface. You can imagine collection, list, set, etc. aggregate is their simplified version. There are three main methods in the container class interface: add object method, remove object method, and iterator method iterator. Iterator is an iterator interface. It mainly has two methods: obtain the iteration object method next and judge whether to complete the iteration method hasnext. You can compare Java util. List and Java util. The two interfaces of iterator think for themselves.

Advantages of iterator mode:

The traversal method is simplified, and it is still troublesome for traversing the object set. For arrays or sequence tables, we can still obtain them through cursors, but users need to traverse the objects themselves on the premise that they know the set well. However, for hash tables, it is troublesome for users to traverse. After introducing the iterator method, it is much easier for users to use. It can provide a variety of traversal methods. For example, for an ordered list, we can provide two iterators: positive traversal and reverse traversal. Users only need our implemented iterators to traverse the set conveniently. The encapsulation is good. Users only need to get the iterator to traverse, and don't care about the traversal algorithm.

Disadvantages of iterator mode:

For relatively simple traversal (such as arrays or sequence tables), it is cumbersome to use iterators. Everyone may feel that, like ArrayList, we prefer to use for loops and get methods to traverse the collection.

Applicable scenarios of iterator pattern

Iterator mode is symbiotic and co dead with sets. Generally speaking, as long as we implement a set, we need to provide iterators for the set at the same time, such as collection, list, set, map, etc. in Java. These sets have their own iterators. If we want to implement such a new container, of course, we also need to introduce the iterator pattern to implement an iterator for our container.

However, because the relationship between container and iterator is too close, most languages provide iterators when implementing containers, and the containers and iterators provided by these languages can meet our needs in most cases. Therefore, it is rare for us to practice iterator mode by ourselves, We just need to use the containers and iterators already in the language.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>