HomePHP 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.
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.