Iterator mode--headfirst Design Patterns Learning Notes

Source: Internet
Author: User

Iterator pattern: Provides a way to sequentially access individual elements in an aggregated object without exposing their internal representations

Characteristics:

    • Decouple the way the client and the object collection are stored-that is, when the client traverses the object without knowing how the object is stored
    • Encapsulates a changing part, that is, a different traversal of multiple different sets, creating an iterator that encapsulates the process of iterating through each collection of objects
    • Customers don't need to know how a collection of specific objects is stored, or how iterators are implemented, simply traverse through an iterator
    • Encapsulates the traversal
    • Resolves an issue that relies on the storage of specific object collections and on specific object types

Attention:

    • For iterators that do not support the Remove method, you can throw java.lang.UnsupportedOperationException
    • Iterators do not imply a sequential
    • You can implement the previous () method to forward the object

External iterator vs Internal iterator:

    • External: The customer takes the next element through next ()
    • Internal: The customer passes the specific action to the iterator, and the specific tuning is controlled by the iterator itself

Single Responsibility Principle: a class should have only one reason for change

    • Assigning a responsibility to a class
    • Each responsibility for a class has a potential area of change, and more than one responsibility means more than one area of change
    • For ease of maintenance, design single-responsibility high-clustering (designed to complete one or a group of highly correlated functions)

Example:

Restaurant for breakfast and lunch menu items of the underlying storage method, if not any optimization, the waiter traversing the menu needs to use different traversal method according to the specific menu storage, but also for the modification of the open way is inconvenient for maintenance, is not conducive to the expansion

We take the changed parts out, create an iterator object with a single responsibility to implement the traversal menu, and encapsulate the way the menu is stored so that the waiter does not need to know, decouple with the waiter

Iterator interface:

1  Public Interface Iterator {  2     boolean  hasnext ();   3     Object Next ();   4 }  

Dinner Menu iterator implementation, for its underlying array storage method:

1  Public classDinermenuiteratorImplementsIterator {2 menuitem[] items; 3     intPosition = 0; 4    5      Publicdinermenuiterator (menuitem[] items) {6          This. Items =items; 7     }  8    9      PublicObject Next () {TenMenuItem MenuItem =Items[position];  OnePosition = position + 1;  A         returnMenuItem;  -     }   -     the      Public BooleanHasnext () { -         if(Position >= Items.length | | items[position] = =NULL) {   -             return false;  -}Else {   +             return true;  -         }   +     }   A}

The waiter traverses the menu through the iterator:

1  Public classWaitress {2 Menu Dinermenu;3      PublicWaitress (Menu dinermenu) {4          This. Dinermenu =Dinermenu;5     } 6      Public voidPrintmenu () {7Iterator Dineriterator =dinermenu.createiterator ();8 9System.out.println ("/nlunch"); Ten Printmenu (dineriterator); One     }  A     Private voidPrintmenu (Iterator Iterator) { -          while(Iterator.hasnext ()) { -MenuItem MenuItem =(MenuItem) Iterator.next (); theSystem.out.print (Menuitem.getname () + ",");  -System.out.print (Menuitem.getprice () + "--");  - System.out.println (Menuitem.getdescription ()); -         }  +}

Iterator mode--headfirst Design Patterns Learning Notes

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.