Tài liệu The iterator pattern pptx

68 452 0
Tài liệu The iterator pattern pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[...]... each iterator is independent, so multiple iterators can operate at the same time Additionally, the iterator operates on the collection as it existed at the time the iterator was requested If another item is added to the collection at any time later, you must request another iterator to display it (at least in this implementation) Continue enhancing the test suite by adding assertions to the TestGetGofIterator()... to the previous “release” iterator, but using the foreach control structure to loop The new iteratorType() method (Listing 20) lets you switch which style of iterator you want to use (Since the iterator type isn’t chosen during the instantiation of the object and because you can choose a different iterator type on -the- fly by calling the iteratorType() method again, the code is actually implementing the. .. (the library itself) to the constructor instead of $this->collection (the array containing the Media collection) in the Library::getIterator() method The “external” iterator would then just track a pointer internally to know which element of the Library collection it’s currently referencing, and would use the reference to the Library passed in the constructor to call the get() method to retrieve the. .. 2001)); } function TestGetGofIterator() { $this->assertIsA($it = $this->lib->getIterator() ,’LibraryGofIterator’); } } Here’s the implementation class Library { 17 FEA T URE The Iterator Pattern // function getIterator() { return new LibraryGofIterator($this->collection); } } The getIterator() method passes the Library’s $collection to the constructor of the new concrete iterator This technique has... ‘LibraryGofIterator’; } return new $iterator_ class($this->collection); } } 19 FEA T URE The Iterator Pattern Here, Library::getIterator() now accepts a parameter to select what kind of iterator to return The default is LibraryGofIterator (so the existing tests still pass Passing the string media to the method creates and returns a LibraryIterator instead This is some code to implement [LibraryIterator]:... portion of the collection An iterator can also show the collection in a specific order Let’s create an iterator that sorts the Media in the collection by release date For a test (Listing 12), add some Media instances with dates older that those of the items added in the setUp() method If the iterator works, these older items should be sorted to the beginning of the iteration This test uses the items... ‘onetwothree’ The reset() function restarts iteration to the beginning of the array; current() returns the value of the current element; and next() advances to the next element in the array and returns the new current() value When you advance past the end of the array, next() returns false Using these iteration methods, the internal implementation of a PHP array is irrelevant to you Iterator couples the object-oriented... you’re sorting is the list of keys for the collection When usort() is complete, the keys will be sorted in order by the year attribute of each object in the collection In the next() method, an object in the collection is accessed via the get() method, but indirectly through the $sorted_keys mapping If you recall the external version of the GoF-style iterator, arrays with gaps or strings in the keys could... Below is the code for the StandardLibraryIterator The code in Listing 18 should look familiar: essentially, it’s a copy of the five SPL functions from the ForeachableLibrary class The tests pass OK, the code is more complex now, but how does it support additional iterator types? Let’s add a test for a “released” version of the iterator to see how additional iterator types work in this design The test... pattern using the smaller interface As a convenience, change the Library::getIterator() method to a parameterized FactoryMethod so you can get either the four-method iterator or the two-method iterator (next() and reset() from that single method) class Library { // function getIterator($type=false) { switch (strtolower($type)) { case ‘media’: $iterator_ class = ‘LibraryIterator’; break; default: $iterator_ class . Design Patterns, you'll learn about the Iterator pattern, whether custom-built, or with PHP 5's new Standard PHP Library. The Iterator Pattern by. manipulate the individual members of the collection. Let’s move on to the purpose of the article, imple- mentation of the Iterator design pattern. The following

Ngày đăng: 24/01/2014, 14:20

Từ khóa liên quan

Mục lục

  • jul05_cover_spread_pdf.qxd

  • php|architect - February 2005

    • Features

      • The Iterator Pattern

      • PHP Library for Permissions Management

      • Change Your Life with Version Control

      • Creating a Simple Image Gallery

      • Departments

        • Editorial

          • pear upgrade Home_Residence

          • What's New!

          • Tips & Tricks

            • Input Filtering: Part 1

            • Test Pattern

              • Not Just Nouns

              • Product Review

                • FPDF: PDF Generation Library

                • Security Corner

                  • Theory

                  • Exit(0);

                    • Forget Viagra, Get Regex!

Tài liệu cùng người dùng

Tài liệu liên quan