[Design mode] iterator Mode)

Source: Internet
Author: User

Abstract:

1. This article will introduce in detail the principles of the iterator mode and the Application in actual code, especially the Android system code.

Outline:

1. Introduce the iterator Mode

2. Introduction to the concepts and advantages and disadvantages of the iterator

3. Application of the iterator in Android source code

1. segment 404 not found :

The iterator (iterator) mode really cannot find the field, but fortunately this mode is not only very easy to understand, but also has many examples. Cnblogs blogger kanonamoocs: If you want to ask the most widely used mode in Java, the answer is not the singleton mode, nor the factory mode, or the strategy mode, but the iterator mode. Let's learn it together.

2. Introduction to the iterator Mode 2.1 What is an iterator Mode?

For collection (or container) such as collection, list, set, MAP, etc.Provides unified traversal InterfacesTo improve code reuse and hide internal representation.

2.2 iterator Mode What are the benefits/ Disadvantage?

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

Advantages:

1. 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.

2. Multiple traversal methods can be provided. For example, for an ordered list, we can provide forward traversal and reverse traversal as needed. Users only need to get the iterator we have implemented, you can easily traverse the set.

3. Good encapsulation. You only need to get the iterator to traverse, but do not need to worry about the traversal algorithm.

Disadvantages (original blog ):

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

This is not accurate because Java provides the traversal syntax for classes that implement the iterator mode:

// Type is a specific type, such as string. C is the container that implements the iterator.

for(Type t : c) {    // 对t的操作...}

It is convenient and safe.

2.3 structure of iterator mode:

(Reference http://blog.csdn.net/zhengzhb/article/details/7610745)

UML:

Abstract container: generally an interface that provides an iterator () method to obtain the iterator instance, 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 () and remove () from the current object ().

Iterator implementation: implements the methods defined in the iterator interface to complete the iteration of the set.

Java has implemented the iterator mode and provided a variety of collection classes for implementing the iterator. Therefore, we basically do not need to create a new class by ourselves, it is sufficient to directly use the classes provided by Java.

3. Source Code practice: 3.1 environment:

The new scaner (system. In) is used to obtain input from the command line window. However, this class is basically not used in Android, because our applications interact with buttons through screens, unless you write Java commands running in shell. Worker implements the iterator interface and has three methods: hasnext, next, and remove. It is an iterator, and this isUse the iterator directly.

Generally, we use the following methods:

while(mScanner.hasNext()) {String str = (String)mScanner.next();}
3.2 arraylist:

Arraylist inherits the List class and implements the iterable interface (different from the iterator interface above ). Note that iterable is implemented for the container class, indicating that I am a class that can "generate" The iterator; The iteator interface is implemented for the iterator, indicating that I am an iterator. The most common method of arraylist:

List<String> mArrayList = new ArrayList<String>();mArrayList.add(a);mArrayList.add(b);mArrayList.add(c);……//遍历for(String a : mArrayList) {    System.out.println(a);}

Of course, you can also use the traditional method to traverse:

Iterator it = mArrayList.iterator();while(it.hasNext()){    String str = (String)it.next();    System.out.println(str);}

There is no difference between the two.

Copyright. For more information, see the source:

Http://www.cnblogs.com/sickworm/p/4016002.html

[Design mode] iterator Mode)

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.