In addition to the design principles in Java, there are also 23 design patterns.
These patterns have been accumulated by the predecessors bit by bit, have been improving, and have been optimizing, and these design patterns can solve some specific problems.
And in these modes, it can be said that the use of language is fully reflected.
Then we are going to learn the iterator mode in the behavioral mode today !
Iterator mode
Let’s Wikipedia first, what is iterator mode
This is actually similar to the iterators we use when traversing List and Set collections. We can traverse the collection without exposing the inside of the object.
The definition and characteristics of the iterator pattern
The definition of iterator mode: Provide an object to sequentially access a series of data in the aggregate object without exposing the internal representation of the aggregate object. The iterator pattern is an object behavioral pattern, and its main advantages are as follows.
1. Access the content of an aggregate object without exposing its internal representation.
2. The traversal task is completed by the iterator, which simplifies the aggregation class.
3. It supports traversing an aggregation in different ways, and can even customize the subclass of the iterator to support new traversals.
4. It is very convenient to add new aggregation classes and iterator classes, without modifying the original code.
5. Good encapsulation, providing a unified interface for traversing different aggregation structures.
The main disadvantage is: increasing the number of classes, which increases the complexity of the system to a certain extent.
The structure and implementation of the iterator pattern
This mode is achieved by separating the traversal behavior of the aggregate object and abstracting it into an iterator class. Its purpose is to allow external code to transparently access the internal data of the aggregate without exposing the internal structure of the aggregate object. .
Let’s take a look at the characters
1. Abstract aggregation role: define the interface for storing, adding, and deleting aggregation objects and creating iterator objects.
2. Concrete aggregation role: implement an abstract aggregation class and return an instance of a concrete iterator.
3. Abstract iterator role: defines the interface for accessing and traversing aggregate elements, usually including methods such as hasNext(), first(), next().
4. Specific iterator 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.
After knowing this, we will simulate the iterator in Java by ourselves and implement one by ourselves
first step:
We first define the abstract aggregation role, which is an object at the bottom
Of course, we don’t have this thing here, let’s let him report the error first, don’t worry about him
The second step:
Let's get a specific implementation class to implement the interface just now
But we don't care where the error is reported
third step:
Let's get an IteratorForMe interface
As you can see, the error message is all OK at the moment, and then we define some methods for this interface
the fourth step:
We will implement the above interface
First of all, when we instantiate this class, we have to pass the collection we want to traverse to him, so we need a construction method to accept this collection
With this, we began to write the specific implementation method inside
the fifth step:
Here we go to complete the getIterator method of ListAggregate
test:
Next we get a test class, we test this iterator
Run it now
OK, that's it, everyone take a good look. Practice a lot. If you have any questions, please contact me QQ: 2100363119
Welcome everyone to visit my personal website: lemon1234.com Thank you for leaving a message