Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Object Interaction and Mediator Classes in PHP 5
  2. Expanding the initial mediator class
  3. Building some additional file handling classes
  4. Implementing the mediator pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 20, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: