CodeQ

Sabtu, 12 Desember 2009

Implementasi NullIterator di PHP

A NullIterator is a degenerate iterator that's helpful for handling boundary conditions. By definition, a NullIterator is always done with traversal; that is, its IsDone operation always evaluates to true.
NullIterator can make traversing tree-structured aggregates (like Composites) easier. At each point in the traversal, we ask the current element for an iterator for its children. Aggregate elements return a concrete iterator as usual. But leaf elements return an instance of NullIterator. That lets us implement traversal over the entire structure in a uniform way.
class Mox_Util_NullIterator implements Iterator, Countable 
{
 function __construct(){}
 
 function rewind(){}
 
 function next() {}

 function valid()
 {
  return false;
 }

 function key()
 { 
  return null;
 }

 function current()
 { 
  return null;
 }

 function count()
 {
  return 0;
 }
}

0 Komentar:

Poskan Komentar

Berlangganan Poskan Komentar [Atom]



<< Beranda