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.
If youíre a PHP developer who has a significant background in developing object-based applications, then possibly youíll agree with me that when it comes to implementing an approach for decoupling the logic of a given class from its concrete implementation, there are some trusted techniques that can be applied successfully.
Among others, the factory pattern is one of the cases that best applies the concept that I described above. When this pattern is used, itís possible to create a class that simply ďreturnsĒ objects to calling code, without having to worry about how these objects behave inside the context of a given application or even how long they persist during the request of multiple PHP files.
However, and now speaking more specifically, using a factory class inside an application implies that you can decouple the logic that stands behind all the created objects from the corresponding creator. At the same time, this means that multiple objects (at least two) are involved in the creation process. Without a doubt, the situation that I illustrated a few lines ago should bring up the following question: is it feasible to work with only one class (not many classes) that can be decoupled from its concrete implementation?
Fortunately, the answer to the above inquiry is a loud "Yes!" To give you an example covering the topic in question, think about an abstract class, where its implementation is most of the time separated from its declaration. That sounds really good, and indeed the application of abstract classes is an important link in the chain of object-oriented programming, but definitely itís not the only approach used for separating a class from its implementation.
Assuming that youíre interested in learning an alternative approach for achieving the mentioned decoupling process, let me now introduce the concept of ďbridgeĒ classes, or whatís more generally known as the bridge pattern. In this specific case, itís possible to create a class where its respective abstract functionality and implementation reside on different class hierarchies, in this way allowing you to decouple the class in question from its concrete application. And best of all, the referenced procedure can be performed without using abstract classes at all.
Of course, Iím pretty certain that all this theory on applying the bridge pattern seems to be a hard thing to grasp, but in fact itís much simpler than you think. Thus, in this series, which is comprised of three tutorials, Iím going to teach you how to create and use bridge classes with PHP 5, by showing you numerous hands-on examples that can be easily included in your own PHP applications with minor modifications.
Ready to learn more on how to apply the bridge design pattern with PHP? All right, letís no waste more time in preliminaries and begin now!