As I said in the section that you just read, demonstrating the functionality of the prototype pattern, in this case implemented as part of a basic data validation application, is only a matter of building a simple PHP script that puts all the prototype classes to work together. Basically, what I'm going to illustrate here is that it's possible to work with a cloned instance of a specific validation object to check whether or not a particular input entry is considered offending. Naturally, the proper verification of incoming data will be performed by implementing the programmatic model dictated by the prototype pattern. In this way you can see more clearly how it works in this concrete case. All right, now that I have explained the purpose of coding a hands-on example, please pay attention to the following code listing. It shows this handy pattern in action. Here it is: try{ /* displays the following: // create new 'AlphanumericValidatorPrototype' object /* displays the following: // create new 'NumericValidatorPrototype' object /* displays the following: Wasn't the above example easy to grasp? I bet it was! As you can see, the example first creates the corresponding prototype objects associated with every data checking class, and then uses the PHP built-in "clone()" method to create one instance of them. Next, these cloned instances perform a simple validation process on a bunch of primitive sample data, and finally an indicative message is displayed on the browser according to the results of this procedure. A final note before you finish reading this article: I recommend that you use all of the prototype classes that I included here as a starting point for understanding more clearly how the prototype pattern works. Of course, you may want to introduce your own modifications to the classes or even better, develop your own functional examples. As with many other topics associated specifically to PHP programming, it's the best way to learn. Final thoughts Unfortunately, we've come to the end of this series. I hope that all the examples shown in this group of articles will be useful enough to demonstrate in a friendly fashion how the prototype pattern can be implemented with PHP 5. As you have seen, there are design patterns that are more unusual than others, and this is precisely the case with the prototype pattern. However, it doesn't hurt to learn at least its basic principles, and eventually how it can be used to solve specific programming-related issues. See you in the next PHP tutorial!
blog comments powered by Disqus |