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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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{
   // get different type of file data
   $numData=file_get_contents('data1.txt');
   $alphaData=file_get_contents('data2.txt');
   // instantiate 'FileHandlerMediator' class
   $fileHandlerMediator=new FileHandlerMediator
($numData,$alphaData);
   // instantiate file handler classes
   $numericFileHandler=$fileHandlerMediator-
>getNumericFileHandler();
   $alphabeticFileHandler=$fileHandlerMediator-
>getAlphabeticFileHandler();
   // display data of different file handlers
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';

   /* displays the following
   12345
   This is alphabetic data
   */

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

   /* displays the following
   12345
   THIS IS ALPHABETIC DATA
   */

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

   /* displays the following
   12345
   this is alphabetic data
   */

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

   /* displays the following
   12345
   THIS IS ALPHABETIC DATA
   */
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

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
   

PHP ARTICLES

- 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: