As I stated at the end of the previous section, I want to finish this tutorial by showing you an instructive example where all the classes that were built previously are put to work in conjunction. Doing so, I'm sure that you'll have a much better idea of how the mediator pattern works. Now, assuming that there are two sample text files called "data1.txt" and "data2.txt," where the first one contains only the trivial numeric value 12345, and the second one holds the string "This is alphabetic data," here's the sample code that illustrates how any change introduced by one data file handler will also be updated on the other one, via the respective mediator. Having said that, take a look at the following code listing: try{ /* displays the following // uppercase numeric file data (also changes the case of other /* displays the following // lowercase numeric file data (also changes the case of other /* displays the following // uppercase alphabetic file data (also changes the case of /* displays the following As you can see, the example shown above demonstrates clearly the principle that drives the mediator pattern. In all the cases where one file handler changes the case of its data, this modification is also introduced by the other handler via the mediator object. Now do you see how a mediator class can be used to keep a group of classes synchronized? I bet you do! As usual, feel free to modify the source code of all the classes shown here. In this way you can develop your own hands-on examples, and eventually acquire a more complete understanding of how the mediator pattern works. Final thoughts In this first part of the series, I walked you through the basics of implementing the mediator pattern with PHP 5. Nonetheless, this journey has another chapter. In the last article I'm going to expand the original file handler mediator class that you learned here to demonstrate how it can be used to synchronize three classes simultaneously. I don't think you'll want to miss it!
blog comments powered by Disqus |