Object Interaction and Mediator Classes in PHP 5

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.

If you already went through the first tutorial of the series, then quite possibly the mediator pattern will be quite familiar to you. However, if it doesn’t ring any bells, let me offer you a quick introduction to how it works. This will let you can grasp more easily the purpose of the hands-on examples that I plan to include in this final article of the series.

Essentially, when the mediator pattern is implemented in the context of a given PHP application, there’s a class (the mediator) which behaves as a centralized mechanism that maintains a complete synchronization between two or more additional classes. These classes are structured in such a way that all the tasks that they perform are handled via the respective mediator.

Logically, the rather confusing character of the previous definition implies that the topic must be complemented with a decent number of practical examples. These will hopefully serve to help you understand the logic that drives the mediator pattern more easily.

Therefore, keeping in mind the strong educational sense that the subject really deserves, in the first article of the series I built a mediator class which was provided with the ability to keep a couple of data file handling classes entirely synchronized. This meant that when one class changed the case of the data it handled, the other one also reflected this modification via the mediator. Quite good, right?

Okay, at this stage I believe that implementing the mediator pattern with PHP 5 should be a pretty painless process to you, but if the examples that I showed you in the first article weren’t enough to help you completely grasp this pattern, over the course of this last tutorial I’m going to expand the original mediator class that you learned previously. The expansion will make it capable of maintaining three classes in perfect synchronization.

Well, with the preliminaries out of our way, let’s learn more examples of how to apply the mediator pattern with PHP 5. Let’s go!

{mospagebreak title=Expanding the initial mediator class}

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.

{mospagebreak title=Building some additional file handling classes}

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.

{mospagebreak title=Implementing the mediator pattern}

As I stated in the section that you just read, here is the pertinent example. It shows how the mediator that was defined previously is used by the respective file handlers to change the case of the data that correspond to each of the objects.

Having said that, the code sample is as follows:

try{
   // get different type of file data
   $numData=file_get_contents(‘data1.txt’);
   $alphaData=file_get_contents(‘data2.txt’);
   $alphanumData=file_get_contents(‘data3.txt’);
   // instantiate ‘FileHandler’ class
   $fileHandlerMediator=new FileHandlerMediator
($numData,$alphaData,$alphanumData);
   // instantiate file handler classes
   $numericFileHandler=
$fileHandlerMediator->getNumericFileHandler();
   $alphabeticFileHandler=
$fileHandlerMediator->getAlphabeticFileHandler();
   $alphanumericFileHandler=
$fileHandlerMediator->getAlphanumericFileHandler();
   // display data of different file handlers
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';
   echo $alphanumericFileHandler->getFileData().'<br />';
           

   /* displays the following
   12345
   This is alphabetic data
   This is line 1 of alphanumeric data
   */

   // uppercase numeric file data (also changes the case of other file data)
   $numericFileHandler->uppercaseFileData();
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';
   echo $alphanumericFileHandler->getFileData().'<br />';

   /* displays the following
   12345
   THIS IS ALPHABETIC DATA
   THIS IS LINE 1 OF ALPHANUMERIC DATA
   */

   // lowercase numeric file data (also changes the case of other file data)
   $numericFileHandler->lowercaseFileData();
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';
   echo $alphanumericFileHandler->getFileData().'<br />';

   /* displays the following
    12345
    this is alphabetic data
    this is line 1 of alphanumeric data
   */           

   // uppercase alphabetic file data (also changes the case of other file data)
   $alphabeticFileHandler->uppercaseFileData();
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';
   echo $alphanumericFileHandler->getFileData().'<br />';

   /* displays the following
   12345
   THIS IS ALPHABETIC DATA
   THIS IS LINE 1 OF ALPHANUMERIC DATA
   */

   // lowercase alphanumeric file data (also changes the case of other file data)
   $alphanumericFileHandler->lowercaseFileData();
   echo $numericFileHandler->getFileData().'<br />';
   echo $alphabeticFileHandler->getFileData().'<br />';
   echo $alphanumericFileHandler->getFileData().'<br />';

   /* displays the following
   12345
   this is alphabetic data
   this is line 1 of alphanumeric data
   */
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Pretty good, isn’t it? As you can see, the previous example demonstrates in a clear way how each change introduced by a particular file handler is reflected and updated by the others, in this manner achieving perfect synchronization across all the involved objects.

Logically, this updating process is performed via the respective mediator, thus I believe that the above example should be more than enough to illustrate the functionality provided by this helpful pattern.

Final thoughts

Sad but true, we’ve come to the end of this series. Hopefully, after reading the two tutorials that comprise it, you’ll have a more solid grounding in how the mediator pattern works, and how it can be implemented quickly with PHP 5.

See you in the next PHP tutorial!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri