In 1980s, there was an "old" TV set in my house. I forgot the brand. I only remember a black and white TV set with no remote control, the buttons on the TV must be used for each switch or switch. What impressed me most is the button used for the switch, you need to rotate it with your hands in person (you need to make some effort to screw it). Every time you turn it, you will snap a bang, if you don't receive any TV channels, there will be a dazzling Snowflake. Of course, the two antennas on the TV can move between the front and back, and become longer and shorter are also one of the iconic components of the TV, I remember that when I was a child, I had to draw those two antennas every time I painted a TV. Otherwise, I always thought it was not a TV ,. With the rapid development of science and technology, more and more advanced TV sets have emerged, and the old TV sets are rarely seen. Compared with the TV set at that time, one of the greatest conveniences that TV sets bring to us today is to increaseTV remote controlYou do not need to directly operate the TV set when starting, shutting down, changing the station, or changing the volume. You can use the remote control to achieve this. We can regard TV sets as a collection object for storing TV channels. Through the remote control, we can operate on TV channels in TV sets, for example, return to the previous channel, jump to the next channel, or jump to the specified channel. Remote control brings great convenience for us to operate TV channels, and users do not need to know how these channels are stored in the TV. TV remote control and TV 1 are shown below:
Figure 1 TV remote control and TV
In software development, there are also a large number of classes similar to televisions. They can store multiple Member objects (elements). These classes are usually called aggregate classes ), the corresponding object is called an aggregate object. In order to operate these aggregation objects more conveniently and flexibly add different traversal methods for the aggregation objects, we also need the same role as the TV remote control, you can access elements in an aggregate object without exposing its internal structure. The iterator mode we will learn in this chapter provides a remote control for the aggregation object. By introducing the iterator, the client can traverse the members of the aggregation object without having to know the internal structure of the aggregation object, you can also easily add new traversal methods as needed.
1. Data traversal in the Sales Management System
Sunny software company developed a sales management system for a mall. When analyzing and designing the system, developers of Sunny software company often need to traverse the product data and customer data in the system. to reuse the traversal code, sunny developers designed an abstract data collection class named abstractobjectlist, the class that stores products, customers, and other data is used as its sub-class. The class structure of abstractobjectlist 2 is shown below:
Figure 2 abstractobjectlist Class Structure In Figure 2, the list object objects is used to store data. The methods are described in table 1: Table 1 abstractobjectlist class method description
Method Name |
Method description |
Abstractobjectlist () |
Constructor, used to assign values to objects |
Addobject () |
Add Element |
Removeobject () |
Delete Element |
Getobjects () |
Get all elements |
Next () |
Move to the next element |
Islast () |
Determines whether the current element is the last element. |
Previous () |
Move to the previous Element |
Isfirst () |
Determines whether the current element is the first element. |
Getnextitem () |
Get next element |
Getpreviusitem () |
Obtain the previous Element |
The productlist and mermerlist subclasses of the abstractobjectlist class are used to store product data and customer data respectively. |
By analyzing the structure of the abstractobjectlist class, developers of Sunny software company found that the design solution has the following problems:
(1) In the class diagram shown in figure 2, addobject (), removeobject () and other methods are used to manage data, while next (), islast (), previous (), isfirst () and other methods are used to traverse data. This will lead to excessive responsibilities of the aggregation class. It is responsible for both data storage and management and data traversal, in violation of the "single responsibility principle", because the aggregation class is very large, the implementation code is too long, it also makes testing and maintenance more difficult.
(2) If an abstract aggregate class is declared as an interface, a large number of methods are filled with this interface, which is not conducive to subclass implementation and violates the "interface isolation principle ".
(3) If all traversal operations are handed over to sub-classes for implementation, the sub-class code will be huge, and the internal storage details of abstractobjectlist must be exposed to the sub-classes to disclose their private attributes, otherwise, the subclass cannot traverse the data, which compromises the encapsulation of the abstractobjectlist class.
How can we solve the above problems? One solution isExtract the methods used to traverse data in the aggregation class and encapsulate them into specialized classes to achieve data storage and data traversal separation. You can perform operations on the aggregation class without exposing the internal attributes of the aggregation class.And this is exactly the intent of the iterator mode.
【Author】 Liu WeiHttp://blog.csdn.net/lovelion]