PHP
  Home arrow PHP arrow Page 3 - 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 - Building a pair of data file handling classes
    ( Page 3 of 4 )

    After showing the pertinent structure of the mediator class that I built in the preceding section, the next step consists of creating the two additional data file classes that you learned previously. As you'll certainly recall, these classes will be kept completely synchronized by the corresponding mediator, a fact that was demonstrated when I showed you its signature.

    And speaking of signatures, here are the ones that correspond to the pair of data file handlers used by the mediator class. Have a look at them, please:

    // define abstract 'FileHandler' class
    abstract class FileHandler{
       private $fileHandlerMediator;
       public function __construct($fileHandlerMediator){
         $this->fileHandlerMediator=$fileHandlerMediator;
       }
       public function getFileHandlerMediator(){
         return $this->fileHandlerMediator;
       }
    }

    // define 'NumericFileHandler' class
    class NumericFileHandler extends FileHandler{
       private $status;
       public function __construct($fileData,$fileHandlerMediator){
         if(!is_numeric($fileData)){
           throw new Exception('File Data must be numeric!');
         }
         $this->fileData=$fileData;
         parent::__construct($fileHandlerMediator);
       }
       public function getFileData(){
         return $this->fileData;
       }
       public function getStatus(){
         return $this->status;
       }
       public function setStatus($status){
         $this->status=$status;
       }
       public function uppercaseFileData(){
         $this->fileData=strtoupper($this->fileData);
         $this->setStatus('uppercased');
         $this->getFileHandlerMediator()->modifyFileData($this);
       }
       public function lowercaseFileData(){
         $this->fileData=strtolower($this->fileData);
         $this->setStatus('lowercased');
         $this->getFileHandlerMediator()->modifyFileData($this);
       }          
    }

    // define 'AlphabeticFileHandler' class
    class AlphabeticFileHandler extends FileHandler{
       private $status;
       public function __construct($fileData,$fileHandlerMediator){
         if(!preg_match("/[a-zA-Z]/",$fileData)){
           throw new Exception('File Data must be numeric!');
         }
         $this->fileData=$fileData;
         parent::__construct($fileHandlerMediator);
       }
       public function getFileData(){
         return $this->fileData;
       }
       public function getStatus(){
         return $this->status;
       }
       public function setStatus($status){
         $this->status=$status;
       }
       public function uppercaseFileData(){
         $this->fileData=strtoupper($this->fileData);
         $this->setStatus('uppercased');
         $this->getFileHandlerMediator()->modifyFileData($this);
       }
       public function lowercaseFileData(){
         $this->fileData=strtolower($this->fileData);
         $this->setStatus('lowercased');
         $this->getFileHandlerMediator()->modifyFileData($this);
       }          
    }

    As shown above, after defining an abstract file handler class on top of the corresponding hierarchy, I derived two subclasses from it, to have at our disposal a couple of objects which are capable of handling numeric and alphabetic data respectively.

    Of course, despite the nature of the data handled by each of these child classes, you should notice that both of them accept the mediator as part of their incoming parameters, which is also used by the respective "uppercaseFileData()" and "lowercaseFileData()" methods that belong to the classes in question. Quite simple to understand, isn't it?

    At this moment, the programmatic model dictated by the mediator pattern should be pretty clear to you, since in the previous chapter you learned how a mediator object can be capable of maintaining at least two additional classes in perfect synchronization, while in this section you saw how these classes use the mentioned methods as a centralized mechanism for performing all their tasks.

    So far, so good. Now that you hopefully grasped how all the previously defined classes work, it's a good time to leap forward and see how these classes can be integrated into an instructive hands-on example, which will help you understand more accurately how the mediator pattern does its business.

    Want to learn how this educational example will be developed? Jump into the upcoming section and keep reading, please.



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek