Home arrow PHP arrow Page 4 - Introducing Mediator Classes in PHP 5

Building a practical example - PHP

The mediator design pattern can help you achieve and maintain synchronization between a group of PHP classes. In this first of a two-part series, you'll be introduced to the mediator pattern, how it functions, and how it can help you with your application development.

  1. Introducing Mediator Classes in PHP 5
  2. Creating a simple mediator class
  3. Building a pair of data file handling classes
  4. Building a practical example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 13, 2007

print this article



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: 

   // get different type of file data
   // instantiate 'FileHandlerMediator' class
   $fileHandlerMediator=new FileHandlerMediator
   // instantiate file handler classes
   // display data of different file handlers
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';

   /* displays the following
   This is alphabetic data

   // uppercase numeric file data (also changes the case of other
file data)
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';

   /* displays the following

   // lowercase numeric file data (also changes the case of other
file data)
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';

   /* displays the following
   this is alphabetic data

   // uppercase alphabetic file data (also changes the case of
other file data)            
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';

   /* displays the following
catch(Exception $e){
   echo $e->getMessage();

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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: