PHP design mode-iterator Mode
Iterator mode: The iterator mode is the mature pattern for traversing the set. The key of the iterator mode is to hand over the task of traversing the set to an object called the iterator, it traverses and selects objects in the sequence during work, and the client programmer does not have to know or care about the underlying structure of the set sequence.
UML class diagram:
Role:
Iterator: The Iterator defines the interface for accessing and traversing elements.
ConcreteIterator (Specific iterator): The iterator implements the iterator interface to track the current position of the aggregation time.
Aggregate (aggregation): Aggregation defines the interface for creating the corresponding iterator object (optional)
ConcreteAggregate (specific aggregation): the interface for creating the corresponding iterator for specific aggregation implementation. This operation returns an appropriate instance of ConcreteIterator (optional)
Core code:
Aggre = $ _ aggre;} // returns the First public function First () {return $ this-> aggre [0];} // returns the Next public function Next () {$ this-> current ++; if ($ this-> current
Aggre) {return $ this-> aggre [$ this-> current];} return false;} // returns whether IsDone public function IsDone () {return $ this-> current> = count ($ this-> aggre )? True: false;} // return the current clustered object public function CurrentItem () {return $ this-> aggre [$ this-> current];}
Call the client test code:
Header (Content-Type: text/html; charset = UTF-8); // -------------------------- iterator mode ------------------- require_once. /Iterator. php; $ iterator = new ConcreteIterator (array ('Jay Chou ', 'faye W', 'chow Yun fa'); $ item = $ iterator-> First (); echo $ item .; while (! $ Iterator-> IsDone () {echo {$ iterator-> CurrentItem ()}: buy a ticket !; $ Iterator-> Next ();}
Use Cases:
1. Access the content of an aggregate object without exposing its internal representation
2. Multiple traversal of aggregate objects
3. provides a unified interface for Traversing different aggregation structures
Welcome to my video course. The address is as follows. Thank you.
PHP Object-Oriented Design Model