PHP
  Home arrow PHP arrow Page 3 - Object Interaction and Mediator Classe...
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

Object Interaction and Mediator Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-03-20

    Table of Contents:
  • Object Interaction and Mediator Classes in PHP 5
  • Expanding the initial mediator class
  • Building some additional file handling classes
  • Implementing the mediator pattern

  • 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


    Object Interaction and Mediator Classes in PHP 5 - Building some additional file handling classes


    (Page 3 of 4 )

    The only things that remains to complete the programmatic model imposed by the mediator pattern is defining the set of classes that are responsible for spawning file handler objects.

    As you saw in the previous section, these objects were conceived to work with specific types of inputs, including numeric and alphabetic data, and alphanumeric values as well. Do you recall that now? I bet you do!

    Okay, now that the functionality of these file handlers are fresh in your mind, please take a look at the group of classes that are responsible for creating these objects. Here they are:

    // 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 alphabetic!');
         }
         $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 'AlphanumericFileHandler' class
    class AlphanumericFileHandler extends FileHandler{
       private $status;
       public function __construct($fileData,$fileHandlerMediator){
         if(!preg_match("/[a-zA-Z0-9]/",$fileData)){
           throw new Exception('File Data must be alphanumeric!');
         }
         $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 you can see, the signatures that correspond to the file handling classes shown above are pretty easy to follow, so I won't spend a long time explaining how each one does its business. However, I'd like you to pay careful attention to the definition of the respective "uppercaseFileData()" and "lowercaseFileData()" methods, which are common to all the classes, since they show in a nutshell the intrinsic functionality of the mediator pattern.

    The aforementioned methods use the mediator object to modify the data manipulated by a particular file handler. This demonstrates clearly how a mediator class can behave as an intermediate entity for handling tasks that need to be performed by other objects.

    At this point, you hopefully understood how all the prior file handlers do their things, therefore the next step to take here consists of grouping all the classes previously defined and setting up a practical example where they can all be put to work together.

    To see how this educational example will be developed, please jump into the next section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this last tutorial of the series, the initial functionality of...
     

       

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