HomePHP Page 4 - Object Interaction and Mediator Classes in PHP 5
Implementing the mediator pattern - 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.
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.