Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build flexible Rails application w/ pattern Bridge (Scale your Rails App #1)

Posted on Oct 3 You're developing your Ruby on Rails app, and you're using SMS Factor as your Sms Sending platform. The code is running, and the SMS sending feature is operational. You're calling your SMS sending class throughout your code. Suddenly, a new opportunity arises. You need to integrate a new SMS provider, Twilio. But how can you add this functionality without having to modify your existing code calls?This is a perfect use case to explore together the Bridge pattern. In this first edition of Scale your Rails App, we will explore how to use the Bridge pattern to overcome this challenge!The Bridge pattern is a structural design pattern that allows you to separate an abstraction from its implementation so that both can evolve independently. In other words, it allows you to define an interface, called the abstraction, which acts as a bridge between your code and specific implementations of external services.Let's imagine that you have a class called SmsSender that you're using throughout your code to send SMS messages.You're calling SmsSender all over your application like this:And you don't want that to change!This is where the Bridge pattern becomes interesting. You can separate the SMS sending logic from SmsSender to make it Provider Agnostic.So, you'll create an Adapter for each provider:And now, all you have to do is change your initial SmsSender class:And voila! The magic happens. All the places where you call SmsSender are unaware of which provider you're using to send your SMS. You can still specify which Adapter you want to use by providing it as an argument during the initialization of SmsSender:The Bridge pattern is highly valuable because it significantly enhances the extensibility of your application.→ If you want to add a new provider:You only need to add a new Adapter. You can set it as the default adapter in the SmsSender class.→ If you want to remove a provider:You can simply delete the Adapter, without even touching SmsSender.And all of this without modifying your calls to SmsSender in the code!Using the Bridge pattern in this situation offers several advantages:The Bridge pattern is a powerful tool for managing external service providers in your Ruby on Rails application. It allows you to maintain a clean and organized codebase while facilitating the integration of new services.I hope this article has inspired you, and you'll apply it in your Ruby on Rails projects for even more efficient and flexible results! 🚀Templates let you quickly answer FAQs or store snippets for re-use.Is it the most common use case ? Do you have another exemples ?Great article :) Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Aniket - Sep 24 Zouhair Sahtout - Sep 15 Akshat Jain - Sep 15 Code_Jedi - Sep 23 Once suspended, justthev will not be able to comment or publish posts until their suspension is removed. Once unsuspended, justthev will be able to comment and publish posts again. Once unpublished, all posts by justthev will become hidden and only accessible to themselves. If justthev is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to V. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag justthev: justthev consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging justthev will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build flexible Rails application w/ pattern Bridge (Scale your Rails App #1)

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×