23 design modes (13): iterator Mode

Source: Internet
Author: User

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

Type:Behavior mode

Class diagram:

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

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

The function of this method is to print a string set cyclically, And the iterator mode is used in it. The Java language has fully implemented the iterator mode, and iterator Translation into Chinese is the meaning of the iterator. When it comes to the iterator, it is first related to the set, which is also called aggregation and container. we can regard the set as a container that can accommodate objects, such as list, set, map, even Arrays can be called collections, and the iterator is used to traverse objects in containers one by one.

 

Structure of the iterator Mode

  • Abstract container: generally an interface that provides an iterator () method, such as the collection interface, list interface, and set interface in Java.
  • Specific containers: specific implementation classes of abstract containers. For example, the List interface achieves arraylist in an ordered list, the list interface implements linklist in a linked list, and the Set interface implements hashset in a hash list.
  • Abstract iterator: defines the methods required for Traversing elements. Generally, there are three methods: Get the first element method first (), get the method next () for the next element (), determine whether to traverse the end method isdone () (or hasnext (), remove the method from the current object remove (),
  • Iterator implementation: implements the methods defined in the iterator interface to complete the iteration of the set.

 

Code Implementation

Interface iterator {public object next (); Public Boolean hasnext ();} class concreteiterator implements iterator {private list = new arraylist (); Private int cursor = 0; public concreteiterator (list) {This. list = List;} public Boolean hasnext () {If (cursor = List. size () {return false;} return true;} public object next () {object OBJ = NULL; If (this. hasnext () {OBJ = This. list. get (cursor ++);} return OBJ;} interface aggregate {public void add (Object OBJ); Public void remove (Object OBJ); Public iterator ();} class concreteaggregate implements aggregate {private list = new arraylist (); Public void add (Object OBJ) {list. add (OBJ);} public iterator () {return New concreteiterator (list);} public void remove (Object OBJ) {list. remove (OBJ) ;}} public class client {public static void main (string [] ARGs) {aggregate Ag = new concreteaggregate (); AG. add ("James"); AG. add ("red"); AG. add ("Xiaogang"); iterator it = Ag. iterator (); While (it. hasnext () {string STR = (string) it. next (); system. out. println (STR );}}}

In the code above, aggregate is a container class interface. You can imagine collection, list, set, etc. Aggregate is their simplified version. There are three methods in the container class interface: add an object method, remove an object method, and obtain the iterator of the iterator method. Iterator is an iterator interface. There are two main methods: Get the iteration object method next, and determine whether the iteration completion method hasnext. You can compare it with Java. util. list and Java. util. two iterator interfaces are self-explanatory.

 

Advantages and disadvantages of the iterator Mode

The iterator mode has the following advantages:

  • The Traversal method is simplified. It is still troublesome to traverse object sets. For arrays or ordered lists, we can still retrieve them through the cursor, however, you need to traverse objects on the premise that you have a clear understanding of the set. However, for hash tables, it is quite troublesome to traverse them. The introduction of the iterator method makes it much easier for users to use.
  • Multiple traversal methods can be provided. For example, for an ordered list, we can provide forward traversal and reverse traversal as needed. To use them, you only need to get the iterator we have implemented, you can easily traverse the set.
  • Good encapsulation. You only need to get the iterator to traverse, but do not need to worry about the traversal algorithm.

Disadvantages of the iterator mode:

  • For relatively simple traversal (such as arrays or ordered lists), it is complicated to use the iterator to traverse data. You may feel like arraylist, we would rather use the for loop and get methods to traverse the set.

 

Applicable scenarios of the iterator Mode

The iterator mode is symbiotic with the set. Generally, as long as we implement a set, we need to provide the set iterator at the same time, just like the collection in Java, list, set, MAP, etc. These sets all have their own iterators. If we want to implement such a new container, we also need to introduce the iterator mode to implement an iterator for our container.

However, because the container and the iterator are too closely related, most languages provide the iterator when implementing the container, in addition, the containers and iterators provided by these languages can meet our needs in most cases. Therefore, it is rare to practice the iterator mode on our own, we only need to use the existing containers and iterators in the language.

From: http://blog.csdn.net/zhengzhb/article/details/7610745

 

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

Type:Behavior mode

Class diagram:

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

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

The function of this method is to print a string set cyclically, And the iterator mode is used in it. The Java language has fully implemented the iterator mode, and iterator Translation into Chinese is the meaning of the iterator. When it comes to the iterator, it is first related to the set, which is also called aggregation and container. we can regard the set as a container that can accommodate objects, such as list, set, map, even Arrays can be called collections, and the iterator is used to traverse objects in containers one by one.

 

Structure of the iterator Mode

  • Abstract container: generally an interface that provides an iterator () method, such as the collection interface, list interface, and set interface in Java.
  • Specific containers: specific implementation classes of abstract containers. For example, the List interface achieves arraylist in an ordered list, the list interface implements linklist in a linked list, and the Set interface implements hashset in a hash list.
  • Abstract iterator: defines the methods required for Traversing elements. Generally, there are three methods: Get the first element method first (), get the method next () for the next element (), determine whether to traverse the end method isdone () (or hasnext (), remove the method from the current object remove (),
  • Iterator implementation: implements the methods defined in the iterator interface to complete the iteration of the set.

 

Code Implementation

Interface iterator {public object next (); Public Boolean hasnext ();} class concreteiterator implements iterator {private list = new arraylist (); Private int cursor = 0; public concreteiterator (list) {This. list = List;} public Boolean hasnext () {If (cursor = List. size () {return false;} return true;} public object next () {object OBJ = NULL; If (this. hasnext () {OBJ = This. list. get (cursor ++);} return OBJ;} interface aggregate {public void add (Object OBJ); Public void remove (Object OBJ); Public iterator ();} class concreteaggregate implements aggregate {private list = new arraylist (); Public void add (Object OBJ) {list. add (OBJ);} public iterator () {return New concreteiterator (list);} public void remove (Object OBJ) {list. remove (OBJ) ;}} public class client {public static void main (string [] ARGs) {aggregate Ag = new concreteaggregate (); AG. add ("James"); AG. add ("red"); AG. add ("Xiaogang"); iterator it = Ag. iterator (); While (it. hasnext () {string STR = (string) it. next (); system. out. println (STR );}}}

In the code above, aggregate is a container class interface. You can imagine collection, list, set, etc. Aggregate is their simplified version. There are three methods in the container class interface: add an object method, remove an object method, and obtain the iterator of the iterator method. Iterator is an iterator interface. There are two main methods: Get the iteration object method next, and determine whether the iteration completion method hasnext. You can compare it with Java. util. list and Java. util. two iterator interfaces are self-explanatory.

 

Advantages and disadvantages of the iterator Mode

The iterator mode has the following advantages:

  • The Traversal method is simplified. It is still troublesome to traverse object sets. For arrays or ordered lists, we can still retrieve them through the cursor, however, you need to traverse objects on the premise that you have a clear understanding of the set. However, for hash tables, it is quite troublesome to traverse them. The introduction of the iterator method makes it much easier for users to use.
  • Multiple traversal methods can be provided. For example, for an ordered list, we can provide forward traversal and reverse traversal as needed. To use them, you only need to get the iterator we have implemented, you can easily traverse the set.
  • Good encapsulation. You only need to get the iterator to traverse, but do not need to worry about the traversal algorithm.

Disadvantages of the iterator mode:

  • For relatively simple traversal (such as arrays or ordered lists), it is complicated to use the iterator to traverse data. You may feel like arraylist, we would rather use the for loop and get methods to traverse the set.

 

Applicable scenarios of the iterator Mode

The iterator mode is symbiotic with the set. Generally, as long as we implement a set, we need to provide the set iterator at the same time, just like the collection in Java, list, set, MAP, etc. These sets all have their own iterators. If we want to implement such a new container, we also need to introduce the iterator mode to implement an iterator for our container.

However, because the container and the iterator are too closely related, most languages provide the iterator when implementing the container, in addition, the containers and iterators provided by these languages can meet our needs in most cases. Therefore, it is rare to practice the iterator mode on our own, we only need to use the existing containers and iterators in the language.

From: http://blog.csdn.net/zhengzhb/article/details/7610745

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.