Singleton is a very important pattern and you should understand properly what it
actually does. You can optimize your application and increase its performance using
this pattern properly.
Iterator Pattern
Iterator is a common pattern, which helps you to manipulate a collection more easily.
Almost every language has built-in support of Iterators. Even PHP5 has a built-in
Iterator objects. Iterators are very useful to provide an easy interface to manipulate a
collection sequentially.
Let us consider this scenario when the Iterator pattern can save the life if a developer
is in complex applications. Let us imagine you are creating a blog, where users write
their daily web logs. How can you display the different posts, one by one?
In the following example you pass all the post_id made by an author in your
template and the template designer writes the following code to display it properly
in the template:
$posts = getAllPosts(); //example function return all post ids of this
author
for($i = 0; $i
{
$title = getPostTitle($post[$i]);
echo $title;
$author = getPostAuthor($post[$i]);
Design Patterns
[ 82 ]
$content = parseBBCode(getPostContent($post[$i]));
echo "Content";
$comments = getAllComments($post[$i]);
for ($j=0; $j{
$commentAuthor = getCommentAuthor($comments[$j]);
echo $commentAuthor;
$comment = getCommentContent($comments[$j]);
echo $comment;
}
}
?>
In this example we do everything in the template; we fetch all post ids, then get
authors, comments, content, and display it.
Pages:
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101