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
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.
Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With