Iterator and Combination Mode

Source: Internet
Author: User

Headfirst Design Model

Iterator mode:

Provides a method to access each element in a Bureau and object in sequence without exposing its internal representation.

(1) custom iterator interface. You need to use the implements class interface of the iterator.

Interface iterator

{

// The two most basic operations required

Object next ();

Boolean hasnest ();

// Optional

Void remove ();

}

(2) Java-provided iterator interface java. util. iterator

***********

Internal iterator: it is controlled by the iterator itself and walk between elements on its own. Therefore, it must tell the iterator what to do during the walk process, that is, to pass the operation to the iterator.

External iterator: the client obtains the next element by using next.

In comparison, the external iterator is more flexible than the internal iterator.

The iterator does not retrieve all elements in sequence, but does not determine the size order of elements based on the order in which they are retrieved.

 

Combination Mode:

Objects can be combined into a tree structure to represent the "whole/part" hierarchy. Combination allows the customer to process individual objects and composite objects in a consistent manner.

After the combination mode is implemented, You need to balance transparency and security as needed.

 

Combined iterator (△)

Here, next and hasnest use stack for recursion. This part is a bit round. After reading it for a long time, the figure will be clear.

/// // The following code is recorded

Public class compositeiterator implements iterator {
Stack stack = new stack (); // each element in the stack is an iterator.
Public compositeiterator (iterator ){
Stack. Push (iterator );
}
Public object next (){
If (hasnext ()){
Iterator = (iterator) stack. Peek (); // view the top elements of the stack.
Menucomponent Component = (menucomponent) iterator. Next (); // retrieves the next element of the top element of the stack.
If (Component instanceof menu ){
Stack. Push (component. createiterator ());
}
Return component;
} Else {
Return NULL;
}
}
Public Boolean hasnext (){
If (stack. Empty ()){
Return false;
} Else {
Iterator = (iterator) stack. Peek ();
If (! Iterator. hasnext ()){
Stack. Pop (); // if no subnode or subnode has been traversed under the top element of the stack, delete the subnode.
Return hasnext (); // then recursively inserts the top element of the next stack.
} Else {
Return true;
}
}
}
Public void remove (){
Throw new unsupportedoperationexception ();
}
}

 

*******

This chapter proposes a new design principle: A class should have only one cause of change.

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.