DEV Community

VzlDev
VzlDev

Posted on

Bridge Pattern

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay