- Intent
- Implement a set of related operations
- Motivation
- Define different iteration strategies, across different inheritance hierarchies, similar to interface, but allows for differnt fuctions to implement the interface specification
- Implementation
- Define a method returning the required functions by the interface
- Examples
class alt_iterator { private $st = array(); function next() { ... } function current() { ... } function valid() { ... } //... put the rest here function protocol() { return array( array($this,'next'), array($this,'current'), array($this,'valid'), ..... ); } } //an example use: iterator protocol function do_them($itor, $callback ) { list($next, $current, $valid, ...) = $itor->protocol(); while($valid) { $callback( $current ); $next(); } }