Bridge Pattern
Explore the bridge pattern to understand how it separates an abstraction from its implementation, allowing independent variation. Learn how this structural design avoids exponential class growth by splitting interfaces for TV and remote models and facilitating flexible, maintainable code.
We'll cover the following...
Overview
According to the Gang of Four books, the bridge pattern is designed to “decouple an abstraction from its implementation so that the two can vary independently.”
This definition can be confusing, so let’s look at a mathematical example.
Let’s suppose that we have two sets:
set1 = (model1remote, model2remote): This is a set of remote models.set2 = (Sony, Benq): This is a set of TVs that we need to make remotes for.
The cartesian product of these two sets is Cartesian Product= {(model1remote,Sony),(model1remote,Benq),(model2remote,Sony), (model2remote,Benq)}. This means that we’ll need each model of the remote for every single TV model. Now, imagine what happens when we increase the length of either of these sets. It will exponentially increase the size of our cartesian product. Let’s map this problem to programming.
In ...