PHP
  Home arrow PHP arrow Page 4 - Introducing Mediator Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing Mediator Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-03-13


    Table of Contents:
  • Introducing Mediator Classes in PHP 5
  • Creating a simple mediator class
  • Building a pair of data file handling classes
  • Building a practical example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introducing Mediator Classes in PHP 5 - Building a practical example
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT