[Design mode] study Note 12: iterator Mode)

Source: Internet
Author: User

From http://blog.csdn.net/shuangde800

 

Enter the iterator Mode

The iterator is almost the most commonly used design mode and is implemented in all object-oriented languages.


1. The iterator first requires an iterator interface:


Public interface iterator {Boolean hasnext (); // a Boolean value is returned, so that we can know whether there are more elements of object next (); // next () the method returns the next element}

Once we have this interface, we can implement the iterator for various object sets: array, list, hash ......


2. encapsulate an array iterator and assume that this array serves the menu of a restaurant:

Public class dinermenuiterator implements iterator {menuitem [] items; int position = 0; Public dinermenuiterator (menuitem [] items) {This. items = items;} // The next () method returns the next item in the array and increments its position to public object next () {menuitem = items [position]; position = Position + 1; return menuitem;} // The hasnext () method checks whether we have obtained all the elements in the array public Boolean hasnext () {If (position> = items. length | items [position] = NULL) {return false;} else {return true ;}}}

3. Combine the iterator into the menu.

Public class dinermenu implements menu {static final int max_items = 6; int numberofitems = 0; menuitem [] menuitems; Public dinermenu () {menuitems = new menuitem [max_items];} public void additem (string name, string description, Boolean vegetarian, double price) {menuitem = new menuitem (name, description, vegetarian, price); If (numberofitems> = max_items) {system. err. println ("sorry, m Enu is full! Can't add item to menu ");} else {menuitems [numberofitems] = menuitem; numberofitems = numberofitems + 1;} public menuitem [] getmenuitems () {return menuitems ;} // This method gets the menu iterator public iterator createiterator () {return New dinermenuiterator (menuitems);} // other menu methods here}

5. to print out the content of this menu, you only need to get its iterator to traverse all menu items and output:

Public class waitress {// This method creates an iterator for each menu. Public void printmenu () {iterator dineriterator = dinermenu. createiterator (); // obtain the iterator printmenu (dineriterator); // call printmenu to traverse the iterator output content} private void printmenu (iterator) {While (iterator. hasnext () {menuitem = (menuitem) iterator. next (); system. out. print (menuitem. getname () + ","); system. out. print (menuitem. getprice () + "--"); system. out. println (menuitem. getdescription ());}}}

By introducing the iterator, the menu implementation has been encapsulated. The waitress class does not know how the menu stores the menu item set.

With the iterator, we only need a loop to process the set of any items in a multi-state manner.

Besides, the waitress class currently only uses one interface (iterator)


Define iterator Mode

The iterator mode provides a way to access each element in an aggregate object sequentially, rather than exposing its internal representation.


The iterator pattern allows us to move on to no element in the aggregate without exposing its internal representation.

Place the walk task on the iterator instead of aggregation. This simplifies the aggregation interface and implementation, and also enables different responsibilities.



Design Principle: single responsibility

A class should have only one reason for change.


We know that we should avoid changes in the class, because modifying the code can easily cause many potential errors. If a class has two reasons for changes, this will increase the probability of changes to the class in the future. When it actually changes, two aspects of your design will be affected at the same time.



Cohesion: Used to measure a class or template to closely achieve a single purpose or responsibility. When a template or a class design only supports a set of related functions, we say it has high cohesion; otherwise, it has low cohesion.


The iterator means there is no order. Only retrieve all elements. It does not indicate that the order of elements is the size order. For the iterator, the data structure can be ordered, there can be no order, or even data can be duplicated. The size order of elements retrieved by the iterator is not assumed unless otherwise specified.




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.