Design Patterns Simplified - Part 11 (Bridge)

I am here to continue the discussion around Design Patterns. Before starting with Part 11, let's first look at the previous articles of the series.

Today we will go through one of the structural design patterns called Bridge. Before talking about its implementation let’s begin with defining it.

As per GOF guys, Bridge Pattern is defined as follows.

Decouple an abstraction from its implementation so that the two can vary independently.”

Well! Let’s understand what they mean and where this pattern can fit into.

In a case when there is more than one version of abstract methods and also many ways to implement them then providing brand new concrete classes of each abstract version may end up with lots of classes. Bridge pattern addresssethe same problem by introducing an interface which works as a bridge between abstract and concreate classes.

Below is the UML for Bridge pattern.

uml

Bridge pattern has four key elements as in the following.

  • Abstraction - Client facing class. Keeps the reference of an object of type Implementor.
  • RefinedAbstraction - Extends the Abstraction class to have multiple version of abstract methods.
  • Implementor or Bridge - This interface acts as bridge between an abstraction classes and concreate implementation classes.
  • ConcreteImplementor -This class implements the Implementor interface.

How Bridge Pattern works

We will understand this by simple example.

Let’s assume a scenario when you have couple of types of email to send and have several ways to do it and at the same time, you want to give choice to client to choose the specific type of email and way or method to send it.

This is the perfect example where bridge pattern can be fit as it allows variation of source (or abstract methods) and target (concreate implementations).

Let’s begin the illustration by creating bridge or Implementor interface.

  1. ///<summary>  
  2. /// The Bridge or Implementor interface  
  3. ///</summary>  
  4. public interface IEmailSender  
  5.   
  6.     void SendEmail(string subject, string body);  
  7. }  
And then the abstract class which defines the business objects and methods as in the following.
  1. ///<summary>  
  2. /// The Abstraction class  
  3. ///</summary>  
  4. public abstract class Email
  5.     public IEmailSenderMessageSender  
  6.     {  
  7.         get;  
  8.         set;  
  9.     }  
  10.   
  11.     public string Subject {  
  12.         get;  
  13.         set;  
  14.     }  
  15.   
  16.     public string Body {  
  17.         get;  
  18.         set;  
  19.     }  
  20.   
  21.     public abstract void Send();  
  22.   
As you can see Email class is holding the reference of IEmailSender to be able to call the appropriate concreate Implementor at runtime. 

Now let’s create the RefinedAbstraction classes which are used to support the multiple versions of abstract methods. Here we have two types of abstractions i.e. SystemEmail and UserEmail.

  1. ///<summary>  
  2. /// The RefinedAbstraction class  
  3. ///</summary>  
  4. public class SystemEmail: Email  
  5.     public override void Send()  
  6.     {  
  7.         string emailSubject = string.Format("Subject: {0} from System", Subject);  
  8.         string emailBody = string.Format("Email Body:\n{0}", Body);  
  9.         MessageSender.SendEmail(emailSubject, emailBody);  
  10.   
  11.     }  
  12.   
  13. }  
  14.   
  15. ///<summary>  
  16.   
  17. /// The RefinedAbstraction class  
  18.   
  19. ///</summary>  
  20.   
  21. public class UserEmail: Email   
  22. {  
  23.     publicoverridevoid Send()  
  24.     { 
  25.         stringemailSubject = string.Format("Subject: {0} from User", Subject);  
  26.   
  27.         stringemailBody = string.Format("Email Body:\n{0}", Body);  
  28.   
  29.         MessageSender.SendEmail(emailSubject, emailBody);  
  30.     }  
  31. }  
And here comes the concrete implementation classes. These classes support the idea of multiple targets.
  1. ///<summary>  
  2. /// The ConcreteImplementor class  
  3. ///</summary>  
  4. public class WebServiceEmailSender: IEmailSender  
  5. {  
  6.     publicvoidSendEmail(string subject, string body)  
  7.     {  
  8.         Console.WriteLine("Sending Email using WebService:\n{0}\n{1}\n", subject, body);  
  9.     }  
  10. }  
  11.   
  12. ///<summary> 
  13. /// The ConcreteImplementor class  
  14. ///</summary>  
  15.   
  16. public class WCFEmailSender: IEmailSender  
  17. {  
  18.     public void SendEmail(string subject, string body)  
  19.     {  
  20.         Console.WriteLine("Sending Email using WCF:\n{0}\n{1}\n", subject, body); 
  21.     } 
  22. }  
  23.   
  24. ///<summary>  
  25.   
  26. /// The ConcreteImplementor class  
  27.   
  28. ///</summary>  
  29.   
  30. public class WebAPIEmailSender: IEmailSender  
  31.     public void SendEmail(string subject, string body) 
  32.     { 
  33.         Console.WriteLine("Sending Email using Web API:\n{0}\n{1}\n", subject, body);  
  34.     }   
  35. }  
As you can see above that we have given three types of concrete implantations to client to choose from. 

So at this point, we are done with required setups. Now let’s use that in client and see the action.

  1. Console.Title = "Bridge pattern demo";  
  2.   
  3. IEmailSenderwebService = newWebServiceEmailSender();  
  4.   
  5. IEmailSenderwcf = newWCFEmailSender();  
  6.   
  7. IEmailSenderwebApi = newWebAPIEmailSender();  
  8.   
  9. //System Email  
  10.   
  11. Emailemail = newSystemEmail();  
  12.   
  13. email.Subject = "Test Message";  
  14.   
  15. email.Body = "Hi there, This is a Test Message from System";  
  16.   
  17. email.MessageSender = webService;  
  18.   
  19. email.Send();  
  20.   
  21. email.MessageSender = wcf;  
  22.   
  23. email.Send();  
  24.   
  25. email.MessageSender = webApi;  
  26.   
  27. email.Send();  
  28.   
  29. //User Email  
  30.   
  31. email = newUserEmail();  
  32.   
  33. email.Subject = "Test Message";  
  34.   
  35. email.Body = "Hi there, This is a Test Message from Prakash";  
  36.   
  37. email.MessageSender = webApi;  
  38.   
  39. email.Send();  
Output

output

You can see by using Bridge pattern how smoothly the client can select the version of abstract methods (between SystemEmail and UserEmail) and concreate implementations (among WebService, WCF and Web API).

Note:

Sometimes people get confused with the purpose of Bridge pattern with Adapter. Please note that Adapter pattern is used to make two incompatible interfaces to work which are having similar functionality but their method names or return types are different. On the other side, Bridge pattern is used when client needs a choice to select from a variety of abstract methods and concrete implementations and both abstract methods and concrete implementations can be modified independently.

Hope you have liked the article. I leook forward for your comments/suggestions.

 
Read more articles on Design Patterns


Similar Articles