Detailed explanation of iterator mode of Android programming design mode

This paper illustrates the iterator pattern of Android programming design pattern. Share with you for your reference, as follows:

1、 Introduction

Iterator pattern, also known as cursor pattern, is one of the behavioral design patterns. Iterator pattern is an old design pattern, which originates from the access to containers, such as list, map, array, etc. we know that the access to container objects will inevitably involve traversal algorithms. We can encapsulate the traversal methods in containers or do not provide traversal methods. If we encapsulate the traversal method into the container, it will assume too many functions for the container class. The container class should not only maintain its own internal data elements, but also provide external traversal interface methods, because the storage of traversal state can not perform multiple traversal operations on the same container at the same time, If we don't provide the traversal method and let the user implement it, the internal details of the container will be exposed. Therefore, the iterative mode came into being. An iterator, a third party, is inserted between the customer access class and the container body, which solves the disadvantages described above.

2、 Definition

Provides a method to sequentially access the elements in a container object without exposing the internal representation of the object.

3、 Usage scenario

Traverse a container object.

4、 UML class diagram of iterator pattern

UML class diagram:

Common mode code:

Iterator interface:

Specific iterator classes:

Container interface:

Specific container types:

Customer category:

Role introduction:

Iterator: iterator interface, which is responsible for defining, accessing and traversing the interface of elements.

Concreteiterator: concrete iterator class. The purpose of concrete iterator class is to implement iterator interface and record the current position of traversal.

Aggregate: container interface, which is responsible for providing an interface for creating specific iterator roles.

Concreteaggregate: a concrete container class with which the concrete iterator role is associated.

Client: customer class.

5、 Simple implementation

Xiaomin and Xiaohui are in the two business departments of the company. One day, the boss arranges a task for them to make statistics on the employee data of their respective departments. It's easy to do. Build a class and save all employee data with the data structure. It's still easy for the boss to use the for loop to implement it when he wants to see it. Let's create an entity class for employees first:

Employee entity class:

Private sector:

Xiaohui Department:

It can be seen that the internal implementation of Xiaomin and Xiaohui is in two ways. The internal essence of Xiaomin's personnel information container is to use a list class to store personnel information, while Xiaohui essentially uses an array. If the boss wants to view personnel information, he must traverse the two containers:

Boss view:

result:

This seems to be no problem, but if there are multiple departments and each department has its own implementation, we need to add traversal logic to the boss class, so that the boss class will have more and more functions and expose internal details. Then we need to define an iterator interface:

Xiaomin's iterator:

Xiaohui's iterator:

Define the interface of the container class:

Modify the previous two container classes:

Boss view:

6、 Iterator mode in Android source code

1、Cursor

When we use the query method of sqlitedatabase to query the database, we will return a cursor object. The essence of the cursor is a specific iterator. We can use it to traverse the result set of database query.

7、 Summary

Since the development of iterator mode, almost all high-level languages have corresponding built-in implementations. For developers, they rarely implement iterators themselves, so this chapter is more about understanding than application.

advantage:

Conform to the principle of single responsibility in object-oriented design principles.

Supports multiple traversal of container objects. The relationship between container class and traversal algorithm is weakened.

Disadvantages:

Addition of class files.

A concurrentmodificationexception occurs.

The traversal process is a one-way and irreversible traversal.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>