PHP design mode-iterator Mode

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.