knowledge/technology/programming/patterns/behavioral/Iterator Pattern.md

17 lines
710 B
Markdown
Raw Normal View History

2023-12-04 10:02:23 +00:00
---
obj: concept
---
# Iterator Pattern
## **Description:**
The Iterator Pattern is a behavioral design pattern that provides a way to access the elements of a collection sequentially without exposing its underlying representation. It defines a common interface for iterating over various types of collections, such as arrays, lists, trees, and more. The pattern separates the responsibility of iteration from the collection itself, promoting encapsulation and flexibility.
## **Example:**
[Rust](../../languages/Rust.md) has iterators which can be extended by functional programming methods like `map()`, etc.
```rust
trait Iterator<T> {
fn has_next(&self) -> bool;
fn next(&mut self) -> Option<T>;
}
```