php迭代器模式(iterator pattern)
。。。
<?php
/*
The iterator pattern is used to traverse a container and access its elements. In
other words, one class becomes able to traverse the elements of another class.
The PHP has a native support for the iterator as part of built in \Iterator and
\IteratorAggregate interfaces.
*/
class ProductIterator implements \Iterator {
private $position = 0;
private $productsCollection;
public function __construct(ProductCollection
$productsCollection) {
$this->productsCollection = $productsCollection;
}
public function current() {
return $this->productsCollection->
getProduct($this->position);
}
public function key() {
return $this->position;
}
public function next() {
$this->position++;
}
public function rewind() {
$this->position = 0;
}
public function valid() {
return !is_null($this->productsCollection->
getProduct($this->position));
}
}
class ProductCollection implements \IteratorAggregate {
private $products = array();
public function getIterator() {
return new ProductIterator($this);
}
public function addProduct($string) {
$this->products[] = $string;
}
public function getProduct($key) {
if (isset($this->products[$key])) {
return $this->products[$key];
}
return null;
}
public function isEmpty() {
return empty($products);
}
}
$products = new ProductCollection();
$products->addProduct('T-Shirt Red<br/>');
$products->addProduct('T-Shirt Blue<br/>');
$products->addProduct('T-Shirt Green<br/>');
$products->addProduct('T-Shirt Yellow<br/>');
foreach ($products as $product) {
var_dump($product);
}
?>
输出:
string(16) "T-Shirt Red
" string(17) "T-Shirt Blue
" string(18) "T-Shirt Green
" string(19) "T-Shirt Yellow
"
php迭代器模式(iterator pattern)的更多相关文章
- 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)
上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- 设计模式系列之迭代器模式(Iterator Pattern)——遍历聚合对象中的元素
模式概述 模式定义 模式结构图 模式伪代码 模式改进 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修 ...
- 迭代器模式(Iterator Pattern)
迭代器模式定义:Iterator Pattern提供一种方法顺序访问一个聚合元素中的各个元素,而又不暴漏内部方法 酒吧提供beer和wine: public class Bar { private L ...
- 16.迭代器模式(Iterator Pattern)
using System; namespace ConsoleApplication9 { class Program { /// <summary> /// 迭代器模式提供了一种方法顺序 ...
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
- [设计模式] 16 迭代器模式 Iterator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...
随机推荐
- git bash 乱码问题之解决方案
解决办法:右击左上方git标识,然后进入到如图中,点击Text,进行操作. 操作完毕后,关闭git bash,然后再重新打开,执行ls或ll命令,查看对应的以中文作为目录或文件名是否显示乱码,如果之前 ...
- web前端图片模糊到清晰的实现过程
在网页图片显示的时候,会发现许多网站采用了先模糊,然后在慢慢清晰的过程,这样的加载用户体验是比较好的,那么如何实现? 默认加载2张图片,一张缩略图,一张原图,当打开网页的时候默认只显示缩略图,然后我们 ...
- 【技术博客】利用Python将markdown文档转为html文档
利用Python将markdown文档转为html文档 v1.0 作者:FZK 元素简单的md文件 Python中自带有一个markdown库,你可以直接这样使用 md_file = open(&qu ...
- 微信公众平台开发(150)——从新浪云SAE上传图片到图文消息
从新浪云SAE上传图片到图文消息,只能用于图文消息中, 没有个数限制 if (!empty($_FILES['qrcode']['name'])){ $filename = time()." ...
- Swagger2.9.2进入API界面报NumberFormatException异常
[转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11778377.html 作者:jstarseven 码字挺辛苦的..... ...
- spark 调优(官方文档)
1.序列化 对象在进行网络传输或进行持久化时需要进行序列化,如果采用序列化慢或者消耗大量字节的序列化格式,则会拖慢计算. spark 提供了两种序列化类库 1). Java serialization ...
- 同级frame之间的通信与跳转
项目最近需求两个同级的frame,通过点击一个frame里面的btn按钮来实现另一个frame的跳转(注意这俩个frame在同一个frameset下面): 原理:通过该frame找到父frameset ...
- System.ValueTuple 未定義或匯入預先定義的類型
System.ValueTuple 没有定义或者导入 'System.ValueTuple´2´ is not defined or imported System.ValueTuple 未定義或匯入 ...
- N-gram理解
如何来理解这个概率呢? p( i love you) 如果是 =p(i)p(love)p(you) 就是只考虑单词出现的概率本身. 如果是 =p(i)p(love|i)p(you|love) 就是 ...
- VC++中双缓冲技术画图
用双缓冲,先在内存中绘制,然后拷贝到屏幕DC,这样就不会出现画出去的情况了,前段时间我也是为这个问题费了不少劲.我把我的一段代码给你看一下: CDC *pDC = m_drawbox.GetDC(); ...