PHP
  Home arrow PHP arrow Page 4 - Introducing Mediator Classes in PHP 5
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over this first article of the series, the logic that drives the mediator pattern is...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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