Object Interaction and Mediator Classes in PHP 5 - Expanding the initial mediator class
(Page 2 of 4 )
As you'll surely recall, in the preceding article of this series I defined a mediator class, which was called "FileHandlerMediator." The purpose of creating this class was to demonstrate the capacity of a mediator structure for synchronizing two different objects.
In this specific case, the objects were conceived as simple file handlers for manipulating numeric and alphabetic data respectively. Nevertheless, I stated in the previous section that my objective was to expand the initial functionality of this file handler mediator class so it could maintain three (not only two) objects in perfect synchronization.
Even though this sounds like a hard thing to accomplish, it's much simpler than you may think. Let me show you the improved signature of this mediator class, this time including the required modifications that allow it to synchronize three different file handlers.
That being said, the definition for this class is as follows:
// define 'FileHandlerMediator' class
class FileHandlerMediator{
private $numericFileHandler;
private $alphabeticFileHandler;
private $alphanumericFileHandler;
public function __construct
($numericData,$alphabeticData,$alphanumericData){
$this->numericFileHandler=new NumericFileHandler
($numericData,$this);
$this->alphabeticFileHandler=new AlphabeticFileHandler
($alphabeticData,$this);
$this->alphanumericFileHandler=new AlphanumericFileHandler
($alphanumericData,$this);
}
public function getNumericFileHandler(){
return $this->numericFileHandler;
}
public function getAlphabeticFileHandler(){
return $this->alphabeticFileHandler;
}
public function getAlphanumericFileHandler(){
return $this->alphanumericFileHandler;
}
// this method lowercase and uppercase respectively the data of the other file handlers
public function modifyFileData(FileHandler $fileHandler){
if($fileHandler instanceof NumericFileHandler){
if($fileHandler->getStatus()=='uppercased'){
if($this->getAlphabeticFileHandler()->getStatus()!
='uppercased'){
$this->getAlphabeticFileHandler()-
>uppercaseFileData();
}
if($this->getAlphanumericFileHandler()->getStatus
()!='uppercased'){
$this->getAlphanumericFileHandler()-
>uppercaseFileData();
}
}
elseif($fileHandler->getStatus()=='lowercased'){
if($this->getAlphabeticFileHandler()->getStatus()!
='lowercased'){
$this->getAlphabeticFileHandler()-
>lowercaseFileData();
}
if($this->getAlphanumericFileHandler()->getStatus
()!='lowercased'){
$this->getAlphanumericFileHandler()-
>lowercaseFileData();
}
}
}
elseif($fileHandler instanceof AlphabeticFileHandler){
if($fileHandler->getStatus()=='uppercased'){
if($this->getNumericFileHandler()->getStatus()!
='uppercased'){
$this->getNumericFileHandler()-
>uppercaseFileData();
}
if($this->getAlphanumericFileHandler()->getStatus
()!='uppercased'){
$this->getAlphanumericFileHandler()-
>uppercaseFileData();
}
}
elseif($fileHandler->getStatus()=='lowercased'){
if($this->getNumericFileHandler()->getStatus()!
='lowercased'){
$this->getNumericFileHandler()-
>lowercaseFileData();
}
if($this->getAlphanumericFileHandler()->getStatus
()!='lowercased'){
$this->getAlphanumericFileHandler()-
>lowercaseFileData();
}
}
}
elseif($fileHandler instanceof AlphanumericFileHandler){
if($fileHandler->getStatus()=='uppercased'){
if($this->getAlphabeticFileHandler()->getStatus()!
='uppercased'){
$this->getAlphabeticFileHandler()-
>uppercaseFileData();
}
if($this->getNumericFileHandler()->getStatus()!
='uppercased'){
$this->getNumericFileHandler()-
>uppercaseFileData();
}
}
elseif($fileHandler->getStatus()=='lowercased'){
if($this->getAlphabeticFileHandler()->getStatus()!
='lowercased'){
$this->getAlphabeticFileHandler()-
>lowercaseFileData();
}
if($this->getNumericFileHandler()->getStatus()!
='lowercased'){
$this->getNumericFileHandler()-
>lowercaseFileData();
}
}
}
}
}
I modified the structure of the previous mediator class to provide it with the capacity for synchronizing three different file handlers. In this case, these file handlers deal with numeric and alphabetic data, and alphanumeric values as well.
It's worth stressing here that each time a file handler object changes the case of the data that it manipulates, this variation is also introduced by the other handlers via the respective mediator. The "modifyFileData()" method, which has been coded above, demonstrates how this synchronization between objects is performed by a few simple PHP sentences.
At this stage, I illustrated in a friendly way how the structure of the mediator class that you learned before can be modified with the purpose of synchronizing three distinct file handlers. However, the signatures of these objects still remain undefined. In the upcoming section I'm going to show you their definitions. Please click on the link below and keep reading.
Next: Building some additional file handling classes >>
More PHP Articles
More By Alejandro Gervasio