Object Interaction and Mediator Classes in PHP 5 - Implementing the mediator pattern
(Page 4 of 4 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |