Home arrow PHP arrow Page 5 - Introducing Bridge Classes with PHP 5

Seeing the bridge pattern in action - 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

Logically, the best way to understand how the bridge pattern works rests on setting up an example where all the classes that were created previously are used together in the same script. In accordance with this, below I coded a few simple snippets which show how the bridge class saves a target object first to an array, then a text file, and finally a cookie. Thus, take a look at the list of try-catch blocks shown below:

try{ // instantiate 'Message' object $msg=new Message('This a test string that will be saved later
on'); // instantiate 'BridgeObjectSaver' object, and indicate that
target object will be saved to array $bObjSaver=new BridgeObjectSaver($msg,'array'); // save target object to array $bObjSaver->save(); } catch(Exception $e){ echo $e->getMessage(); exit(); }           try{ // instantiate 'Message' object $msg=new Message('This a test string that will be saved later
on'); // instantiate 'BridgeObjectSaver' object, and indicate that target object will be saved to file             $bObjSaver=new BridgeObjectSaver($msg,'file');             // save target object to text file             $bObjSaver->save(); } catch(Exception $e){ echo $e->getMessage(); exit(); } try{ // instantiate 'Message' object $msg=new Message('This a test string that will be saved later
on'); // instantiate 'BridgeObjectSaver' object, and indicate that
target object will be saved to a cookie $bObjSaver=new BridgeObjectSaver($msg,'cookie'); // save target object to a cookie $bObjSaver->save(); } catch(Exception $e){ echo $e->getMessage(); exit(); }

As youíll realize, the above examples demonstrate a clear implementation of the bridge pattern with PHP 5. Of course, if youíre interested in learning more on this pattern, feel free to modify the original source code that I showed here, and experiment with developing more complex examples.

Conclusion

In this first part of the series, I introduced the core concepts of the bridge design pattern in PHP 5. As you hopefully learned, this pattern allows you to separate a given class from its corresponding implementation, without using abstract classes.

In the next part, Iím going to show you how to apply this pattern to create different data validation classes. The experience is really promising, therefore you donít have any excuses to miss it!



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