Home arrow PHP arrow Page 3 - Object Interaction and Mediator Classes in PHP 5

Building some additional file handling classes - PHP

Are you looking for an approachable guide on how to use the mediator design pattern in PHP 5? If you answered "yes," then you should take a look at this group of articles. Welcome to the final part of the series that began with “Introducing Mediator Classes in PHP 5.” This series takes you through building mediator classes with PHP, and shows you how to use this useful pattern with copious code samples.

TABLE OF CONTENTS:
  1. Object Interaction and Mediator Classes in PHP 5
  2. Expanding the initial mediator class
  3. Building some additional file handling classes
  4. Implementing the mediator pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 20, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: