PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Creating a simple mediator class
    ( Page 2 of 4 )

    Since the main objective of this series is to demonstrate how the mediator pattern works, the first thing that I'm going to do is build a mediator class. As you'll see shortly, this class will be capable of handling the interaction between two file data handling objects in such a way that if one of them changes the case of the data it manipulates, then this modification will be reflected on the other as well, and vice versa.

    Now that I have explained how this mediator class is going to work, please pay attention to its corresponding signature, which is shown below:

    // define 'FileHandlerMediator' class
    class FileHandlerMediator{
        private $numericFileHandler;
        private $alphabeticFileHandler;
        public function __construct($numericData,$alphabeticData){
          $this->numericFileHandler=new NumericFileHandler
    ($numericData,$this);
          $this->alphabeticFileHandler=new AlphabeticFileHandler
    ($alphabeticData,$this);
        }
        public function getNumericFileHandler(){
          return $this->numericFileHandler;
        }
        public function getAlphabeticFileHandler(){
          return $this->alphabeticFileHandler;
        }
        // this method lowercase and uppercase respectively the data
    of the other file handlers
        public function modifyFileData(FileHandler $fileHandler){
          if($fileHandler instanceof NumericFileHandler){
            if($fileHandler->getStatus()=='uppercased'){
              if($this->getAlphabeticFileHandler()->getStatus()!
    ='uppercased'){
                $this->getAlphabeticFileHandler()->uppercaseFileData
    ();
              }
            }
            elseif($fileHandler->getStatus()=='lowercased'){
              if($this->getAlphabeticFileHandler()->getStatus()!
    ='lowercased'){
                $this->getAlphabeticFileHandler()->lowercaseFileData
    ();
              }
            }
          }
          elseif($fileHandler instanceof AlphabeticFileHandler){
            if($fileHandler->getStatus()=='uppercased'){
              if($this->getNumericFileHandler()->getStatus()!
    ='uppercased'){
                $this->getNumericFileHandler()->uppercaseFileData();
              }
            }
            elseif($fileHandler->getStatus()=='lowercased'){
              if($this->getNumericFileHandler()->getStatus()!
    ='lowercased'){
                $this->getNumericFileHandler()->lowercaseFileData();
              }
            }           
          }                       
        }
    }

    As you can see, the "FileHandlerMediator" class defined above performs a few interesting tasks which deserve a closer look. First, the mediator accepts two incoming parameters called "$numericData" and "$alphabeticData," which are inputted directly into the respective constructors that belong to the data file handling objects that I mentioned a few lines above.

    In addition, an instance of the mediator is also passed to these objects; the reason for doing this will be clear when you see their corresponding signatures. For the moment, I'd like you to focus your attention on the definition of the "modifyFileData()" method, which obviously is the mediator's workhorse.

    As you saw previously, the aforementioned method first takes up a data file handling object as its unique input parameter, and then checks to see whether the case of the data manipulated by the appropriate handler object has changed. If it has, then this modification is extended to the other data file handler too, in this way demonstrating how a mediator can keep at least two classes completely synchronized. Quite good, right?

    All right, now that you have hopefully grasped the programming logic that stands behind the mediator class that you learned a few lines above, it's time to move forward and show the signatures that correspond to the pair of data file handler objects used by the mediator in question. As you'll see in a few moments, learning the definition of these new objects is a crucial step to understanding more clearly how the mediator pattern works.

    To see how these file data handlers will be created, please click on the link that appears below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek