Design Mode Reading Notes ----- iterator Mode

Source: Internet
Author: User

Your project team receives a project: uniformly manages TV channels, movies, and radio menus, and establishes a unified menu management interface, all TV, movie, and radio channels are displayed. You have three employees: Xiao Li, Xiao xinzi, and Xiao Anzi. They develop each module to see what they have done.

This is the main menu JavaBean, used to display the menu of each module.

   MenuItem(.name =.description =.channe = .name = .description =   setChanne(.channe =

Mr. Li is responsible for implementing the TV channel menu. It is implemented by using List, and he thinks it is very convenient to extend the menu.

  TVChanneMenu <MenuItem>    =  ArrayList<MenuItem>1, "CCTV-1", "This is CCTV-1"2, "CCTV-2", "This is CCTV-2"3, "CCTV-3", "This is CCTV-3"4, "CCTV-4", "This is CCTV-4"5, "CCTV-5", "This is CCTV-5"      addItem(=  List<MenuItem>

Xiao Xingzi is responsible for the development of the movie menu module. She uses arrays. He thinks the array is more efficient and can control the menu length.

FilmMenu MAX_ITEMS = 5; numberOfItems = 0 = 1, "peerless", "this is starring Bruce wlis... "2," Da Vinci Code "," This is one of my favorite movies... "3," hacker empire 123 "," Do you know what you can understand ??? "4," my girlfriend is a robot "," a classic love movie that I don't dislike... "5," Xiao shenke's salvation "," Freedom, happiness, how far are you "addItem (= (numberOfItems>" sorry, the menu is full .... "= ++

Here we will only introduce two. They have implemented their respective menu functions. When you display three menus in a unified manner, you must call their getMenuItems () to obtain the values in each menu, however, the problem occurs at this time, and you find that their return values are different. You must do this to demonstrate it.

List<MenuItem> tvMenuItems =( i = 0 ; i < tvMenuItems.size() ; i++== =( i = 0 ; i < filmItems.length ; i++=

Here we always need to process these two menus and traverse these menu options through loops. What if the implementation of Xiao Anzi is different? Do you need three loops? What if I need to add a module next time? Here you get annoyed, why don't they adopt the same implementation method, but you cannot let them change it at this time, they have too much code dependent on these two menus. If they are changed, they will need to rewrite a lot of code. So you just want to encapsulate the loop? Nice.Encapsulation Traversal. This is the motivation of the iterator model --It can walk through every element in the aggregation and provide different traversal methods.

I. Schema Definition

What is the iterator mode?The so-called iterator mode provides a method to access each element in an aggregate object sequentially, rather than exposing its internal representation.. In the actual development process, we may need to address different needs, and may need to traverse the entire integrated object in different ways,We do not want to fill the abstract interface layer of the aggregate object with various convenient operations.. At this time, we need such a thing. It should have the following three functions:

1. It facilitates an aggregation object.

2. We do not need to understand the internal structure of the aggregate object.

3. provides different traversal methods.

These three functions are the problems to be solved in the iterator mode. As a powerful mode,In the iterator mode, the responsibility for migrating between elements is handed over to the iterator, instead of aggregating objects.. This simplifies the aggregation interface and implementation, and enables aggregation to focus more on what it should be focused on.Compliant with the single Responsibility Principle.

Ii. pattern structure

Is an iterator mode UML diagram.

Then there are two specific iterators. An iterator traverses the TV interface and an iterator traverses the movie interface.

Film Festival target iterator: FilmMenuIterator. java

  FilmMenuIterator  position = 0.menuItems = (position > menuItems.length-1 || menuItems[position] ==   =++

TV interface iterator: TVChanneMenuIterator. java

  TVChanneMenuIterator <MenuItem> position = 0 TVChanneMenuIterator(List<MenuItem>.menuItems = (position > menuItems.size()-1 || menuItems.get(position) ==   =++

Then there is the menu interface, which provides the method to return a specific iterator: createIterator ().

   addItem(

Two aggregate classes. The two aggregation classes implement the createIterator () method, which returns the TV interface aggregation class and the movie interface aggregation class respectively.

FilmMenu MAX_ITEMS = 5; numberOfItems = 0 = 1, "peerless", "this is starring Bruce wlis... "2," Da Vinci Code "," This is one of my favorite movies... "3," hacker empire 123 "," Do you know what you can understand ??? "4," my girlfriend is a robot "," a classic love movie that I don't dislike... "5," Xiao shenke's salvation "," Freedom, happiness, how far are you "addItem (= (numberOfItems>" sorry, the menu is full .... "= ++

 

  TVChanneMenu <MenuItem>    =  ArrayList<MenuItem>1, "CCTV-1", "This is CCTV-1"2, "CCTV-2", "This is CCTV-2"3, "CCTV-3", "This is CCTV-3"4, "CCTV-4", "This is CCTV-4"5, "CCTV-5", "This is CCTV-5"      addItem(=  

Now we can implement the main menu to display and traverse all the TV and movie interfaces.

. TvMenu =. filmMenu = "TV shows:" "------------------------------------------------------------------" movie shows: "=" channe: "+ menuItem. getChanne () + "," "name:" + menuItem. getName () + "," "description:" +

Test procedure:

   = = = 

Running result

Iv. Advantages and Disadvantages

1. It supports traversing an aggregate object in different ways.

2. The iterator simplifies the aggregation class.

3. Multiple traversal can be performed on the same aggregation.

4. In the iterator mode, it is convenient to add new aggregate classes and iterator classes without modifying the original code.

Disadvantages

Because the iterator mode separates the responsibilities of storing and traversing data, a new aggregation class needs to be added to the new iterator class, and the number of classes increases in pairs, this increases the complexity of the system to a certain extent.

V. Use Cases

1. Access the content of an aggregate object without exposing its internal representation.

2. Multiple traversal methods must be provided for aggregate objects.

3. provides a unified interface for Traversing different aggregation structures.

Vi. Summary

1. The iterator mode provides a way to access an aggregate object without exposing its internal representation.

2. extract the data behavior in the traversal aggregate object, encapsulate it into an iterator, and use a dedicated iterator to traverse the internal data of the aggregation object, this is the essence of the iterator mode. The iterator mode is the perfect embodiment of the "single responsibility principle.

3. When an iterator is used, we rely on Aggregation to provide traversal.

4. The iterator provides a common interface that allows us to traverse aggregate items. When we encode and use aggregate items, we can use the polymorphism mechanism.

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.