Home>Article>Backend Development> A brief discussion of the iterator pattern in PHP
In the previous article "In-depth analysis of the observer pattern in PHP" we introduced the observer pattern in PHP. The following article will take you to understand the iterator pattern in PHP.
When talking about this pattern, we have to mention loop statements. In "Dahua Design Pattern", the author said that the learning significance of this model is now greater than the practical significance. Why is this? Of course, it was because of foreach. Any language has this kind of similar syntax that can quickly and easily traverse arrays and objects, thus allowing the iterator pattern to slowly become a passer-by from the star among the 23 major design patterns. Especially our PHP language, the power of PHP lies in its flexible operation of arrays. It is a hashmap structure. Naturally, there are various convenient array operation syntaxes, and foreach is also our most commonly used statement, even better than for. Also commonly used.
GoF definition: Provides a method to sequentially access each element in an aggregate object without exposing it The internal representation of the object
GoF class diagram
##Code implementation
interface Aggregate { public function CreateIterator(); } class ConcreteAggregate implements Aggregate { public function CreateIterator() { $list = [ "a", "b", "c", "d", ]; return new ConcreteIterator($list); } }The first is the aggregate class, which is a class that can be iterated. Because I am an object-oriented design pattern, the iterator pattern is aimed at iterating the contents of a class. Here, in fact, we just simulated an array and handed it over to the iterator.
interface MyIterator { public function First(); public function Next(); public function IsDone(); public function CurrentItem(); } class ConcreteIterator implements MyIterator { private $list; private $index; public function __construct($list) { $this->list = $list; $this->index = 0; } public function First() { $this->index = 0; } public function Next() { $this->index++; } public function IsDone() { return $this->index >= count($this->list); } public function CurrentItem() { return $this->list[$this->index]; } }The iterator makes its debut and mainly implements four methods to operate on collection data. A bit like what you do with cursors when learning data structures or databases. Use First() and Next() to move the cursor, use CurrentItem() to obtain the data content of the current cursor, and use IsDone() to confirm whether there is next data. Therefore, this mode is also called
cursor mode.
$agreegate = new ConcreteAggregate(); $iterator = $agreegate->CreateIterator(); while (!$iterator->IsDone()) { echo $iterator->CurrentItem(), PHP_EOL; $iterator->Next(); }The client can directly use while to operate.
Our mobile phone factory is amazing. It has assembled a production line by itself. What is this production line mainly used for? We have handed over the molding machine to Foxconn, and our line is for painting mobile phones. When we put all the delivered mobile phones (Aggregate) into different production lines (Iterator), they will help us paint the color of the current production line one by one. Isn’t it very powerful! ! Technology is not just about changing shells. If this line is still there, we can do other things, such as adding some lanyards. Anyway, as long as I can pass one by one, I can put things on. Do you think it will work? Easy to use! !
Full code: https://github.com/zhangyue0503/designpatterns-php/blob/master/07.iterator/source/iterator.php
实例还是围绕着我们的短信发送来看。这一次,我们的业务需求是尽快的发一批通知短信给用户,因为活动的时候可不等人啊。在之前我们会使用多个脚本来把用户手机号分成多组来进行发送。现在我们可以用swoole来直接多线程的发送。所要达到的效果其实就是为了快速的把成百上千的短信发完。这个时候我们也会做一些策略,比如数据库里是100条要送的短信,有个字段是发送状态,一个线程正序的发,一个线程倒序的发,当正序和倒序都发送到50条的时候其实已经同步的发完这100条了,不过也有可能会有失败的情况出现,这时,两个线程还会继续去发送那些上次发送不成功的信息,这样能够最大程度的确保发送的效率和到达率。
消息发送迭代器类图
完整源码:https://github.com/zhangyue0503/designpatterns-php/blob/master/07.iterator/source/iterator-msg.php
list = $list; $this->index = 0; } public function First() { $this->index = 0; } public function Next() { $this->index++; } public function IsDone() { return $this->index >= count($this->list); } public function CurrentItem() { return $this->list[$this->index]; } } // 反向迭代器 class MsgIteratorDesc implements MsgIterator { private $list; private $index; public function __construct($list) { // 反转数组 $this->list = array_reverse($list); $this->index = 0; } public function First() { $this->index = 0; } public function Next() { $this->index++; } public function IsDone() { return $this->index >= count($this->list); } public function CurrentItem() { return $this->list[$this->index]; } } interface Message { public function CreateIterator($list); } class MessageAsc implements Message { public function CreateIterator($list) { return new MsgIteratorAsc($list); } } class MessageDesc implements Message { public function CreateIterator($list) { return new MsgIteratorDesc($list); } } // 要发的短信号码列表 $mobileList = [ '13111111111', '13111111112', '13111111113', '13111111114', '13111111115', '13111111116', '13111111117', '13111111118', ]; // A服务器脚本或使用swoole发送正向的一半 $serverA = new MessageAsc(); $iteratorA = $serverA->CreateIterator($mobileList); while (!$iteratorA->IsDone()) { echo $iteratorA->CurrentItem(), PHP_EOL; $iteratorA->Next(); } // B服务器脚本或使用swoole同步发送反向的一半 $serverB = new MessageDesc(); $iteratorB = $serverB->CreateIterator($mobileList); while (!$iteratorB->IsDone()) { echo $iteratorB->CurrentItem(), PHP_EOL; $iteratorB->Next(); }
说明
完整源码:https://github.com/zhangyue0503/designpatterns-php/blob/master/06.observer/source/spl_observer.php
彩蛋
PHP中的Iterator接口已经为我们准备好了一套标准的Iterator模式的实现,而且(这里需要画重点),实现这个接口的类可以用foreach来遍历哦!
文档:https://www.php.net/manual/zh/class.iterator.php
源码:https://github.com/zhangyue0503/designpatterns-php/blob/master/07.iterator/source/iterator-php.php
文档中相关的接口都可以看看,更重要的是,PHP的SPL扩展中,也为我们准备了很多常用的迭代器封装。要知道,面试的时候要是能说出这里面的几个来,那面试官可是也会刮目相看的哦!
SPL迭代器:https://www.php.net/manual/zh/spl.iterators.php
本文转载自:https://juejin.cn/post/6844903937921777671
作者:硬核项目经理
推荐学习:《PHP视频教程》
The above is the detailed content of A brief discussion of the iterator pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!