Bridge Pattern

VzlDev - Mar 15 - - Dev Community

Hello, today i'm gonna talk about a structural design pattern, more specifically about the Bridge design pattern and its advantages.

The Bridge Pattern decouples an abstraction from its implementation, allowing them to vary independently. It is particularly useful when there are multiple dimensions of variability in a system. The pattern achieves this by creating two separate class hierarchies: one for the abstraction and another for the implementation. These hierarchies are then connected by composition rather than inheritance.

Let's imagine that we go watch a movie, and the cinema have a promotion that is selling movie licenses, and we have the option to buy a 2 days movie license and a lifetime movie license. In top of that when purchasing these licenses, discounts may be applied, such as "Military discount" and "Senior Discount".
So right now our code structure would look like this:

Image description

But that doesn't seem correct. If the cinema wants to introduce here a 5 day movie license or imagine, a discount for students and another for their own workers, the code structure would be very confuse and hard to maintain.
So we have to separate our concerns and divide the movie license and the movie discount, and our code structure should be something like this:

Image description

This way is very easy to maintain our code and to introduce new licenses or discounts.

. . . . . . . . . .