Pius Adams Ijachi
3 min readJan 24, 2024
iterator pattern

Iterator Pattern

The Iterator pattern is a design pattern that allows you to traverse a collection of objects without exposing its underlying representation. It provides a common interface for accessing the elements of different types of collections, such as arrays, lists, trees, etc.

The Iterator pattern is useful when you want to decouple your logic from the data structure that you are iterating over. For example, you may want to perform some operation on each element of a collection, but you don’t care about how the collection is stored or organized internally. By using an iterator, you can abstract away the details of the collection and focus on the logic of your operation.

One use case for the Iterator pattern is when you want to access data from different social networks, such as Facebook, Twitter, LinkedIn, etc. Each social network may have its own way of storing and representing user profiles, but you want to treat them uniformly and iterate over them in a consistent way. To achieve this, you can use the Iterator pattern to create a common interface for accessing user profiles from different social networks.

To implement the Iterator pattern in PHP, you need to define two interfaces: SocialNetworkInterface and ProfileIteratorInterface. The SocialNetworkInterface defines the methods for creating an iterator for a specific social network. The ProfileIteratorInterface defines the methods for accessing the elements of a collection using an iterator.

Here is an example of how to implement the Iterator pattern for Facebook:

class Facebook implements SocialNetworkInterface
{
public function __construct(public array $profiles)
{
    }    public function getIterator(): ProfileIteratorInterface
{
return new FacebookIterator($this);
}
}
class FacebookIterator implements ProfileIteratorInterface
{
private int $currentPosition = 0;
    public function __construct(public Facebook $facebook)
{
}
public function hasNext(): bool
{
return isset($this->facebook->profiles[$this->currentPosition]); } public function getNext()
{
if ($this->hasNext()) {
return $this->facebook->profiles[$this->currentPosition++];
}
return null;
}
}

The Facebook class represents a collection of user profiles from Facebook. It implements the SocialNetworkInterface and provides a method for creating a FacebookIterator. The FacebookIterator class implements the ProfileIteratorInterface and provides methods for accessing the elements of the Facebook collection.

To use the Iterator pattern, you can create an instance of the Facebook class and pass it an array of user profiles. Then, you can create an iterator from the Facebook instance and use it to traverse the collection. For example:

<?php
use Iterator\Facebook;require_once __DIR__ . '/Facebook.php';$user_profiles = [
[
'name' => 'John Doe',
'email' => 'john@test.com',
],
[
'name' => 'Jane Doe',
'email' => 'jane@test.com',
],
];
$profiles = new Facebook($user_profiles);
$profileIterator = $profiles->getIterator();
while ($profileIterator->hasNext()) {
$profile = $profileIterator->getNext();
echo $profile['name'] . ' - ' . $profile['email'] . PHP_EOL;
}
// output
This code will output:
John Doe - john@test.com
Jane Doe - jane@test.com

The Iterator pattern allows you to iterate over the user profiles from Facebook without knowing or caring about how they are stored or represented internally. You can also easily extend this pattern to other social networks by implementing their own classes and iterators that conform to the same interfaces.

If you want to learn more about the Iterator pattern and see other examples in PHP, you can check out this

GitHub page: https://github.com/Adams-Ijachi/design-patterns

You can also watch this YouTube video that explains the concept and implementation of the Iterator pattern: https://www.youtube.com/watch?v=uNTNEfwYXhI&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc&index=17

Pius Adams Ijachi
Pius Adams Ijachi

No responses yet