Iterator Pattern

Date
2006-07-16 (Sun)
Category
Design Pattern | php

もう今更ですが、PHP 再入門というか、Object 指向再入門してみようと思い立ちました。だいぶ前に買って一度 java でやったのですが、PHP でやってやろうと。

目次通りにやって行くつもりなので、初回はやや簡単ですが、Iterator です。

最初にちょっとだけ詰まったのが、関数の戻り値をどうやって参照で渡す方法でした。もちろん本書では Java でやってるので、値を渡す時は参照が基本だけれども、PHP はそうではないので明示してやる必要があります。マニュアル以下のページに書いてあります。

リファレンスを返す

簡単にまとめると

でした。

あと Iterator.php のコード中にでてくるインターフェイス名が YIterator となっているのは、PHP が内部的に Iterator というクラスを定義しているからです。マニュアルを見て発見したですが、CXLV. Standard PHP Library (SPL) 関数 というクラスライブラリが提供されているのですね…スゴいそろってるじゃん。。

SPL-StandardPHPLibrary Class List

foreach を使った時に、iterate されないクラスメンバとかはこれらを使うと設定できるみたいでした。今回のデザインパターン入門が終わったら見てみたいと思います。

以下が本文登場順のコード。

Aggregate.php

<?php
interface Aggregate
{
	public function iterator();
}
?>

Iterator.php

<?php
interface YIterator
{
	public function hasNext();
	public function & next();
}
?>

Book.php

<?php
class Book
{
	private $name;
	public function __construct($name) {
		$this->name = $name;
	}
	public function getName() {
		return $this->name;
	}
}
?>

Bookshelf.php

<?php
class BookShelf implements Aggregate 
{
	private $books;
public function __construct() { $this->books = array(); } public function & getBookAt( $index ) { return $this->books[$index]; } public function appendBook( &$book ) { array_push($this->books, $book); } public function getLength() { return count($this->books); } public function iterator() { return new BookShelfIterator($this); } } ?>

BookShelfIterator.php

<?php
class BookShelfIterator implements YIterator
{
	private $bookshelf;
	private $index;
	public function __construct( &$bookshelf ) {
		$this->bookshelf = $bookshelf;
		$this->index = 0;
	}
	public function hasNext() {
		if ($this->index < $this->bookshelf->getLength()) {
			return true;
		} else {
			return false;
		}
	}
	public function & next() {
		$book = $this->bookshelf->getBookAt($this->index);
		$this->index++;
		return $book;
	}	
}
?>

main.php

<?php
require_once('Aggregate.php');
require_once('Iterator.php');
require_once('Book.php');
require_once('BookShelf.php');
require_once('BookShelfIterator.php');
$bookshelf = new BookShelf(); $bookshelf->appendBook( new Book('Around the World in 80 Days') ); $bookshelf->appendBook( new Book('Bible') ); $bookshelf->appendBook( new Book('Cinderella') ); $bookshelf->appendBook( new Book('Daddy-Long-Legs') ); $it = $bookshelf->iterator();
while($it->hasNext()) { $book = $it->next(); echo $book->getName()."\n"; } ?>

実行するにはファイルを同じディレクトリに保存して、php main.php としてください。以下が実行結果。

$ php main.php 
Around the World in 80 Days
Bible
Cinderella
Daddy-Long-Legs

Comment:23

cheap bridesmaid dresses under 100:2011-07-25 (Mon) 02:39

Well,OK.I have to say,what a wonderful blog it is.thank you for your sharing so good articles in the website.I like it very much.It is very interesting in it.Look forward to reading so good articles in the website

Cheap nike shox:2011-08-20 (Sat) 05:14


Our Nike Shox Outlet was the famous brand for men and women all over the world. Nike Shox have the great market as its good quality and reasonable price for many people. During you doing sports,Nike Shox Shoes Outlet give you superior comfortable and flexibility. The mesh lining above shoes and give you breathable wearing feeling. As a world famous sports brand,Nike Shox included Mens Nike Shox NZ and Womens Nike Shox NZ are very lightweight and durable. So you can choose these Nike Shox shoes by yourself and you will become the fashionable style. Cheap Nike Shox with excellent function will never let you down. Take action to select

Diablo 3 power leveling:2011-09-17 (Sat) 03:52

asked the table if not cartier replica Shenzhen Longio dive 1000 meters jewelry line Each carefully selected .

Ugg Classic tall online:2011-09-18 (Sun) 21:44

The article in your blog reminds me some old memory .That is good .It gives me happy .I think we will have a harmonious talk.Do you agree?

air max shoes:2011-09-30 (Fri) 00:37

Hi, friends, sports you like, then to set the professional bar equipment. Really good.

christian louboutin outlet:2011-10-17 (Mon) 22:31

I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often. Took me awhile to read all the comments, but I really love the article.

Wholesale jerseys :2011-10-27 (Thu) 21:32


Writing a one of a kind and informative article like this deserves a bunch of constructive comments (which is actually obvious).Nice work!

wedding dress:2011-10-28 (Fri) 04:22

I like your article so that I read all of your articles in a day.Please continue and keep on writing excellent posts.

Aaron Rodgers Jersey:2011-11-07 (Mon) 03:05

thank you for share the good knowlege. So beautiful posts,all I like,hope to be better then,
and enjoy yourself,good luck in your life. Thanks for your ideas to something. Great post,
thanks very much, please write more and more about this.

Packers Jersey:2011-11-07 (Mon) 03:06

thank you for share the good knowlege. So beautiful posts,all I like,hope to be better then,
and enjoy yourself,good luck in your life. Thanks for your ideas to something. Great post,
thanks very much, please write more and more about this.

flyers Jersey:2011-11-07 (Mon) 03:07

Your blog is very informative.This is obviously one great post.i keep on reading articles from here.
thanks for sharing..

Charles Woodson Jersey:2011-11-07 (Mon) 03:08

I really like this website, And hope you will write more ,thanks a lot for your information.

B.J. Raji Jersey:2011-11-07 (Mon) 03:10

I’m still learning from you, but I’m improving myself.

Clay Matthews Jersey:2011-11-07 (Mon) 03:11

I definitely liked reading everything that is written on your blog.Keep the information coming. I liked it!

Donald Driver Jersey:2011-11-07 (Mon) 03:11

Hey, your website is quite interesting.. And it was something I can definitely connect with Ill
constantly visit in your blog therefore i hope you continue making fun and interesting posts such as this one…

Brett Favre Jersey:2011-11-07 (Mon) 03:12

There are no secrets to success. It is the result of preparation, hard work, and learning from failure.

Office 2007:2011-11-16 (Wed) 20:29

No more switch Office 2007 from view to view in order Microsoft Office 2010 key to determine your availability.

winter clothes:2011-11-28 (Mon) 19:38

I very light given you of the article, very good, top, you a

MBT shoes:2011-11-28 (Mon) 23:17

strict liability ZhuiJiuZhi, malignant MBT Shoes Clearance accidents MBT Shoes Sale happened to serious processing according to law.

cocktail dresses:2011-12-02 (Fri) 04:01

cocktail dresses

cocktail dresses:2011-12-02 (Fri) 04:03

cocktail dresses

Moncler Jackets Men:2011-12-12 (Mon) 07:27

Thank you for calling this matter to our attention.

The North Face Winter Hats:2011-12-23 (Fri) 20:54

If this in fact is with regard to the beyond, you'll need to properly level your current outfits depending on weather factors scenarios. Perfect adding gets to be a lot more

Comment Form

Remember Me?


Trackback:0

TrackBack URL for this entry
http://blogs.grf-design.com/mt/mt-tb.cgi/179
Listed below are links to weblogs that reference
Iterator Pattern from The Croton

Return to Page Top