Building an array Iterator is not a complex task at all. Specifically, the PHP5 SPL (Standard PHP Library) offers a new set of interfaces for implementing Iterators very easily (Iterator, ArrayIterator, and so on), so if you want to go deeper into the topic, visit the PHP site for detailed information. In this case, I won’t use these interfaces for developing an array iterator, since they’re available only in PHP 5. For those developers using PHP 4, coding such a programming structure can be a didactical experience, before deciding to migrate to PHP’s latest version. However, if you’ve crossed the bridge to PHP 5, I strongly recommend using these existing interfaces. Despite the fact that some of the common design patterns applied in software engineering are considered to be within the category of “advanced concepts,” having a decent knowledge of their theory and simple implementation in practical situations, helps you to know how to apply them in real conditions. Implementing the Iterator pattern to traverse a simple array structure may be as simple as this: class arrayProcessor{ Although the code seems to be rather long, it’s quite simple to understand. It simply exposes the methods needed for performing different operations on an array structure. The class presents different methods for getting the current array element, or moving the array pointer back and forth. What’s more, we’re able to count, search and reverse array elements. Since we’ve analyzed the logic of the “arrayProcessor” class, now the reason for using its methods within the “Cache” class should become clear. Because results sets are manipulated as associative arrays, it’s a matter of common sense to utilize an array Iterator for handling them. This is a simple topic, right? All right, now we have seen in detail how these two classes establish a strong interaction and play a relevant role within the caching application. I know that it's easy to say that using smaller and portable classes to build a bigger caching system leads to easier and faster implementation, but if you find yourself writing a large class that does “everything,” reconsider your design approach and start thinking of applications as separated blocks of reusable code that can be easily plugged into each other. Keeping in mind these guidelines within application development, it’s time to put all of the classes together and assemble the whole caching system. Just click to the next page to learn how this is done.
blog comments powered by Disqus |