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

Object Interaction and Mediator Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT