Iterator pattern analysis, structure diagram and basic code, pattern structure

Source: Internet
Author: User

Iterator pattern analysis, structure diagram and basic code, pattern structure

Definition: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
Applicable: When you need to access a clustered object and traverse all these objects, you should consider using the iterator mode. Alternatively, you can use the iterator mode when you need to perform aggregation in multiple ways.
Although we do not need to explicitly reference the iterator, the system still implements traversal through the iterator. In general, the iterator mode separates the traversal behavior of the set object and abstracts an iterator class to take charge of it, so that the internal structure of the set is not exposed, in addition, external code can transparently access the internal data of the set. The iterator mode is widely used when accessing data such as arrays, sets, and lists, especially database data operations.
Structure:


Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace iterator Mode
{
Class Program
{
Static void Main (string [] args)
{
ConcreteAggregate a = new ConcreteAggregate ();

A [0] = "laruence ";
A [1] = "coriander ";
A [2] = "Luggage ";
A [3] = "foreigner ";
A [4] = "Bus internal staff ";
A [5] = "thief ";

Iterator I = new ConcreteIterator ();
// Iterator I = new ConcreteIteratorDesc ();
Object item = I. First ();
While (! I. IsDone ())
{
Console. WriteLine ("{0} buy a ticket! ", I. CurrentItem ());
I. Next ();
}

Console. Read ();
}
}

Abstract class Aggregate
{
Public abstract Iterator CreateIterator ();
}

Class ConcreteAggregate: Aggregate
{
Private IList <object> items = new List <object> ();
Public override Iterator CreateIterator ()
{
Return new ConcreteIterator (this );
}

Public int Count
{
Get {return items. Count ;}
}

Public object this [int index]
{
Get {return items [index];}
Set {items. Insert (index, value );}
}
}

Abstract class Iterator
{
Public abstract object First ();
Public abstract object Next ();
Public abstract bool IsDone ();
Public abstract object CurrentItem ();
}

Class ConcreteIterator: Iterator
{
Private ConcreteAggregate aggregate;
Private int current = 0;

Public ConcreteIterator (ConcreteAggregate aggregate)
{
This. aggregate = aggregate;
}

Public override object First ()
{
Return aggregate [0];
}

Public override object Next ()
{
Object ret = null;
Current ++;

If (current <aggregate. Count)
{
Ret = aggregate [current];
}

Return ret;
}

Public override object CurrentItem ()
{
Return aggregate [current];
}

Public override bool IsDone ()
{
Return current> = aggregate. Count? True: false;
}

}

Class ConcreteIteratorDesc: Iterator
{
Private ConcreteAggregate aggregate;
Private int current = 0;

Public ConcreteIteratorDesc (ConcreteAggregate aggregate)
{
This. aggregate = aggregate;
Current = aggregate. Count-1;
}

Public override object First ()
{
Return aggregate [aggregate. Count-1];
}

Public override object Next ()
{
Object ret = null;
Current --;
If (current> = 0)
{
Ret = aggregate [current];
}

Return ret;
}

Public override object CurrentItem ()
{
Return aggregate [current];
}

Public override bool IsDone ()
{
Return current <0? True: false;
}

}
}


JAVA iterator

Iterator mode .... Iterator

This Iterator object can be returned for any implementation class in the collected class. Just like a loop, the advantage is that it can be applied to any class, and java actually optimizes it, which is a little faster than accessing it directly using index (this cannot be proved, )..

However, it is good to use it ~~ It is better to add a generic type ~~

For example
ArrayList <String> arr = new ArrayList <String> ();
Iterator it = arr. iterator; // it seems to be iterator .. You can see the doc.
This can be done during iteration.
While (it. hasNext ()){
// Do some processing, such
System. out. print (it. Next );
}

In combination with generic, one advantage is that it. next () does not need to be converted ~

I used objects in the past, but I still need to convert them myself. Well, I feel like, Iterator, and generics are just out of stock ~~ It seems no direct benefit to use it alone

Iterator Mode

Yes. You can do additional work in Test1 Test2.

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.