This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money. Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction to other design patterns

Create a type:The factory method The abstract factory The prototype

Structured:The adapter The bridge model Portfolio model Decorative pattern The appearance model The flyweight pattern The proxy pattern

Behavior type:cor The command The interpreter The iterator broker The memo The state pattern The strategy pattern Template method The visitor

define

Provides a way to access individual elements of a container object without exposing the inner details of the object.

If asked about the most used pattern in Java, the answer is not singleton, factory, or policy, but iterator. Let’s start with some code:

 public static void print(Collection coll){
	Iterator it = coll.iterator();
	while(it.hasNext()){ String str = (String)it.next(); System.out.println(str); }}Copy the code

Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator When it comes to iterators, first of all, they are related to collections, which are also called collections, containers, etc. We can regard collections as containers that contain objects, such as List, Set, Map, and even array. The function of iterators is to iterate through the objects in the containers one by one. Structure of the iterator pattern

Abstract container: An interface that provides an iterator() method, such as Collection, List, Set, etc. Concrete container: it is the concrete implementation class of abstract container, such as the ordered List of List interface to achieve ArrayList, the linked List of List interface to achieve LinkList, the hash List of Set interface to achieve HashSet, etc. Abstract iterators: Define the methods needed to traverse an element. Generally, there are three methods: First () to get the first element, next() to get the next element, isDone() (or hasNext()) to determine whether to iterate over the end of the method, remove() to remove the current object, iterator implementation: implement the method defined in the iterator interface, complete the set iteration.

advantages

For array or ordered list, we can still get it by cursor, but the user needs to understand the set clearly, traversal the object, but for hash table, user traversal is more troublesome. With the introduction of the iterator method, it is much easier for users to use.

Can provide a variety of traversal methods, for example, for ordered list, we can provide forward traversal, reverse traversal according to the need of two iterators, users only need to get our good iterator, can easily traversal the set. Good encapsulation, the user only need to get the iterator can traverse, and the traversal algorithm does not care about.

disadvantages

For simple traversal (like arrays or ordered lists), iterator traversal is tedious, as you might expect. Like ArrayList, we prefer to use for loops and get methods to traverse collections.

Applicable scenario

The iterator pattern is co-existing and co-existing with collections. Generally speaking, as long as we implement a Collection, we need to provide iterators of this Collection at the same time, just like Collection, List, Set, Map, etc. In Java, these collections have their own iterators. If we were to implement such a new container, of course we would also need to introduce the iterator pattern to implement an iterator for our container.

But, because of the container and iterator is too close, so far, most of the language at the time of realization of container is to provide an iterator, and these languages provide containers and iterators in most cases can meet the needs of us, so now want to practice the iterator pattern scenarios ourselves is rare, We just need to use containers and iterators that are already in the language.