HomePHP Page 2 - Introducing Mediator Classes in PHP 5
Creating a simple mediator class - 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.
Since the main objective of this series is to demonstrate how the mediator pattern works, the first thing that I'm going to do is build a mediator class. As you'll see shortly, this class will be capable of handling the interaction between two file data handling objects in such a way that if one of them changes the case of the data it manipulates, then this modification will be reflected on the other as well, and vice versa.
Now that I have explained how this mediator class is going to work, please pay attention to its corresponding signature, which is shown below:
// define 'FileHandlerMediator' class class FileHandlerMediator{ private $numericFileHandler; private $alphabeticFileHandler; public function __construct($numericData,$alphabeticData){ $this->numericFileHandler=new NumericFileHandler ($numericData,$this); $this->alphabeticFileHandler=new AlphabeticFileHandler ($alphabeticData,$this); } public function getNumericFileHandler(){ return $this->numericFileHandler; } public function getAlphabeticFileHandler(){ return $this->alphabeticFileHandler; } // 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 (); } } elseif($fileHandler->getStatus()=='lowercased'){ if($this->getAlphabeticFileHandler()->getStatus()! ='lowercased'){ $this->getAlphabeticFileHandler()->lowercaseFileData (); } } } elseif($fileHandler instanceof AlphabeticFileHandler){ if($fileHandler->getStatus()=='uppercased'){ if($this->getNumericFileHandler()->getStatus()! ='uppercased'){ $this->getNumericFileHandler()->uppercaseFileData(); } } elseif($fileHandler->getStatus()=='lowercased'){ if($this->getNumericFileHandler()->getStatus()! ='lowercased'){ $this->getNumericFileHandler()->lowercaseFileData(); } } } } }
As you can see, the "FileHandlerMediator" class defined above performs a few interesting tasks which deserve a closer look. First, the mediator accepts two incoming parameters called "$numericData" and "$alphabeticData," which are inputted directly into the respective constructors that belong to the data file handling objects that I mentioned a few lines above.
In addition, an instance of the mediator is also passed to these objects; the reason for doing this will be clear when you see their corresponding signatures. For the moment, I'd like you to focus your attention on the definition of the "modifyFileData()" method, which obviously is the mediator's workhorse.
As you saw previously, the aforementioned method first takes up a data file handling object as its unique input parameter, and then checks to see whether the case of the data manipulated by the appropriate handler object has changed. If it has, then this modification is extended to the other data file handler too, in this way demonstrating how a mediator can keep at least two classes completely synchronized. Quite good, right?
All right, now that you have hopefully grasped the programming logic that stands behind the mediator class that you learned a few lines above, it's time to move forward and show the signatures that correspond to the pair of data file handler objects used by the mediator in question. As you'll see in a few moments, learning the definition of these new objects is a crucial step to understanding more clearly how the mediator pattern works.
To see how these file data handlers will be created, please click on the link that appears below and keep reading.