HomePHP Page 4 - Introducing Bridge Classes with PHP 5
Implementing the logic of a bridge class in a different hierarchy level - PHP
The bridge class, or what's commonly known as the bridge pattern, lets you create a class with its abstract functionality and implementation residing on different class hierarchies. This lets you decouple the class from its concrete application. This article, the first of three parts, introduces you to the bridge pattern and its uses.
In accordance with the concepts that you learned in the previous section, the “BridgeObjectSaver” class uses others to save target objects either to text files, cookies or arrays. Having at our disposal this handy programmatic structure, the logic that drives the bridge pattern is now easy to follow.
As I stated before, the “BridgeObjectSaver” class resides on a different hierarchy level, since its logic is implemented by other objects, in this case called “ObjectToArray,” “ObjectToFile” and “ObjectToCookie” respectively. Now, do you realize how the pattern in question works? I bet you do!
All right, having explained how the previous bridge class does its business, I’d like you to focus your attention on the signature for the following “bridged” classes that were discussed before. They’re defined as follows:
//define 'ObjectToArraySaver' class
class ObjectToArraySaver{
private $targetArray=array();
// implement concretely the logic of 'save()' method
public function save($messageObj){
$this->targetArray['messageobj']=$messageObj;
}
}
// define 'ObjectToFileSaver' class
class ObjectToFileSaver{
private $targetFile='default_file.txt';
// implement concretely the logic of 'save()' method
public function save($messageObj){
if(!$fp=fopen($this->targetFile,'w')){
throw new Exception('Error opening target
file');
}
if(!fwrite($fp,serialize($messageObj))){
throw new Exception('Error writing data to
target file');
}
fclose($fp);
}
}
// define 'ObjectToCookieSaver' class
class ObjectToCookieSaver{
private $targetCookie='targetCookie';
// implement concretely the logic of 'save()' method
public function save($messageObj){
setcookie($this->targetCookie,serialize($messageObj),
time()+7200);
}
}
As you can see, the three classes shown above now implement concretely the logic defined by “BridgeObjectSaver.” In this case in particular, each one of the previous classes exposes a “save()” method to save target objects to different locations, including text files, arrays and cookies. Quite handy, right?
Okay, at this point I’m quite sure that you grasped correctly the approach used for implementing the bridge pattern with PHP 5. After all, the process is reduced only to defining a few “bridged” classes that perform the tasks defined inside the corresponding bridge.
Now, let’s move forward and see together how all the previous classes can be put to work conjunctly to demonstrate the real capacity offered by the bridge design pattern. Therefore, go ahead and read the final section of the article. I’ll be there, waiting for you.