Certainly, I could spend my time and yours explaining theoretically how the prototype pattern works, but I know that you're expecting to see a concrete example that illustrates how multiple instances of a prototype object can be created during the execution of a specific script. Therefore, I coded a brief example (shown below), which shows in a friendly fashion how this task is performed via the cloning process that I mentioned previously. The source code that corresponds to the example in question looks like this: try{ /* $array2= clone $arrayPrototype; /* // create new 'FilePrototype' object /* As you can see, the above code snippet shows in a nutshell how the prototype pattern works, since it uses the built-in PHP "clone()" method to create different instances of a prototype object. More specifically speaking, the first case demonstrates how to spawn two different array objects by cloning their respective prototype, while the second one uses the same approach, but this time with a pair of file-related objects. Do you grasp the logic followed by the prototype pattern? I bet you do! Finally, my last suggestion concerning the implementation of this unusual pattern doesn't differ too much from using other ones: try testing and tweaking all the classes shown here, to give you a more robust background in how this pattern works. I'm sure you'll have a good time! Final thoughts In this first article of the series I walked you though the basics of implementing the prototype pattern with PHP 5. Hopefully, all the hands-on examples coded here will help you expand your existing skills in pattern-based programming. In the final tutorial of the series, I'm going to demonstrate how to use this handy pattern to develop and expandable data validation application. You've been warned, so don't miss it!
blog comments powered by Disqus |