I do not understand the "bridge" design pattern at all. I have gone through various web sites, but they haven't helped.
Can anybody help me in understanding this?
|
I do not understand the "bridge" design pattern at all. I have gone through various web sites, but they haven't helped. Can anybody help me in understanding this? |
|||||
|
|
While most design patterns have helpful names, I find the name "Bridge" to be unintuitive in regards to what it does. Conceptually, you push the implementation details used by a class hierarchy into another object, typically with its own hierarchy. By doing so, you are removing a tight dependency on those implementation details and allowing the details of that implementation to change. On a small scale, I liken this to the use of a strategy pattern in the way you can plug in a new behavior. But instead of just wrapping an algorithm as is often seen in a stratgey, the implementation object is usually more feature filled. And when you apply the concept to an entire class hierarchy the larger pattern becomes a Bridge. (Again, hate the name). It is not a patten that you will use every day, but I have found it helpful when managing a potential explosion of classes that can happen when you have an (apparent) need for multiple inheritence. Here is a real-world example: I have a RAD tool that lets you drop and configure controls on a design surface, so I have an object model like this:
And so on, with perhaps a dozen controls. But then a new requirement is added to support multiple themes (look-n-feels). Let's say we have the following themes: I could create an object model like this:
etc, for each control type
etc, for each control type (again) You get the idea, you get a class explosion of the # of widgets X # of themes. This complicates the RAD designer by making it aware of each an every theme. Plus, adding new themes forces the RAD designer to be modified. Further, there is a lot of common implementation within a theme that it would be great to inherit, but the controls are already inheriting from a common base ( So instead what I did is create a seperate object heirarchy that implements the theme. Each widget would hold a reference to the object that implements the rendering operations. In many texts, this class is suffixed with an "Impl" but I deviated from that naming convention. So now my TextboxWidget looks like this:
And I can have my various painters inherit my theme-specific base, which I could not do before:
One of the nice things is that I can dynamically load the implementions at runtime, allowing me to add as many themes as I want without changing the core software. In other words my "implementation can vary independent of the abstraction". |
|||
|
|
|
In OOP we use polymophism so an abstraction can have multiple implementations. Lets look at the following example -
A new requirement introduced and need to bring in the acceleration perspective of the trains, so change the code as below.
The above code is not maintainable and lack of reusability (assuming we could reuse the accleration machanism for the same track platform). Following code apply bridge pattern and seperate the two different abstractions, train transport and acceleration.
|
|||
|
|