Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the implementation of the java.util.Iterator use the State pattern?

I am just looking for a reasonable answear how implementation of

java.util.Iterator 

is coming under State Design Pattern

EDIT

Please Refer the below Link

Examples of GoF Design Patterns in Java's core libraries

like image 312
BOSS Avatar asked Jul 21 '11 09:07

BOSS


People also ask

How does an iterator design pattern work?

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

What kind of pattern is iterator pattern?

Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

What is the ability of the iterator pattern?

The key idea of Iterator pattern is to give you do an access and traversal out of the list in the manner you want. All this functionality will be placed in iterator object. The iterator class defines an interface for accessing the elements of the list.


2 Answers

That, I am not sure. As far as I know, its an implementation of Iterator Design Pattern.

However, it can, wrongly, be said that it uses State Pattern, as calling next() affects the state of the Iterator object. But, IMO, its not really a State Pattern implementation, as it doesn't change the base object on which operation has to be executed. Wikipedia is having a fine Java example of State Pattern.

like image 151
Adeel Ansari Avatar answered Sep 25 '22 07:09

Adeel Ansari


To quote from the GoF book:

State: Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class.

This definitely does not sound like an Iterator. Neither are these two patterns mentioned as related in the book, btw.

like image 41
Péter Török Avatar answered Sep 26 '22 07:09

Péter Török