PHP
  Home arrow PHP arrow Page 2 - 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 - 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


       · 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 1 hosted by Hostway
    Stay green...Green IT