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

TABLE OF CONTENTS:
  1. Introducing Mediator Classes in PHP 5
  2. Creating a simple mediator class
  3. Building a pair of data file handling classes
  4. Building a practical example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 13, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: