Home arrow PHP arrow Page 3 - Introducing Mediator Classes in PHP 5

Building a pair of data file handling classes - PHP

The mediator design pattern can help you achieve and maintain synchronization between a group of PHP classes. In this first of a two-part series, you'll be introduced to the mediator pattern, how it functions, and how it can help you with your application development.

TABLE OF CONTENTS:
  1. Introducing Mediator Classes in PHP 5
  2. Creating a simple mediator class
  3. Building a pair of data file handling classes
  4. Building a practical example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 13, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: