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


       · 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