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!