Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Iterator Pattern

Iterator Pattern

The Iterator Pattern (Iterator Pattern) is a very commonly used design pattern in Java and .Net programming environments.

The Iterator Pattern provides a way to sequentially access the individual elements of an aggregate object without exposing its internal representation.

The Iterator Pattern belongs to behavioral patterns.

Introduction

Intent

Allows sequential access to elements of an aggregate object without exposing the object's internal representation.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Supports multiple traversal methods: Different iterators can define different traversal methods.
  2. Simplifies aggregate classes: Aggregate classes do not need to worry about traversal logic.
  3. Multiple traversal support: Multiple traversals of the same aggregate object can be performed simultaneously.
  4. Extensibility: Adding new aggregate classes and iterator classes is convenient, with no need to modify existing code.

Disadvantages

Usage recommendation

Notes

structures

The Iterator Pattern contains the following main roles:

Create a class that implements

We will create an interface describing navigation methods Iterator interface and an interface that returns an iterator Container interface. The entity classes that implement the Container interface will be responsible for implementing the Iterator interface.

IteratorPatternDemo, and our demo class uses the entity class NamesRepository to print the NamesRepository that are stored as a collection in the Names。

Step 1

Create the interface:

Iterator.java

public

interface

Iterator

{

public

boolean

hasNext

(

)

;

public

Object

next

(

)

;

}

Container.java

public

interface

Container

{

public

Iterator

getIterator

(

)

;

}

Step 2

Create concrete classes that implement the Container Entity class of the interface. This class has an inner class that implements the Iterator interface NameIterator。

NameRepository.java

public

class

NameRepository

implements

Container

{

public

String

[

]

names

=

{

"

Robert

"

,

"

John

"

,

"

Julie

"

,

"

Lora

"

}

; @

Override

public

Iterator

getIterator

(

)

{

return

new

NameIterator

(

)

;

}

private

class

NameIterator

implements

Iterator

{

int

index

; @

Override

public

boolean

hasNext

(

)

{

if

(

index

<

names

.

length

)

{

return

true

;

}

return

false

;

}

@

Override

public

Object

next

(

)

{

if

(

this

.

hasNext

(

)

)

{

return

names

[

index

++

]

;

}

return

null

;

}

}

}

Step 3

Use NameRepository to get the iterator and print the names.

IteratorPatternDemo.java

public

class

IteratorPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

NameRepository

namesRepository

=

new

NameRepository

(

)

;

for

(

Iterator

iter

=

namesRepository

.

getIterator

(

)

;

iter

.

hasNext

(

)

;

)

{

String

name

=

(

String

)

iter

.

next

(

)

;

System

.

out

.

println

(

"

Name :

"

name

)

;

}

}

}

Step 4

Run the program, output result:



Name : Robert

Name : John

Name : Julie

Name : Lora