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

TABLE OF CONTENTS:
  1. Introducing Bridge Classes with PHP 5
  2. Introducing the basics of the bridge pattern
  3. Implementing the bridge pattern’s core logic
  4. Implementing the logic of a bridge class in a different hierarchy level
  5. Seeing the bridge pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
January 03, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: