Bridge Pattern

15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

So guys, it's time to move to the new type of design pattern. So, up to this point, whatever design pattern we have seen were actually creational design patterns. And I hope you guys have gone through the creational design patterns and you understood that the concept of the creational design patterns were based on the object relations. Now it's time to move on to the second type of design patterns which is actually structural design pattern. structural design patterns are helping us to create an organized structure in our project code. The first pattern which we will see in this is a bridge pattern.

This bridge pattern is actually used to separate an abstraction from its implementation, so that both can be modified independently. Now up to this point, we know one thing that when we say abstraction, this is going to either an interface, or maybe an abstract class, which is going to connect with multiple concrete implementations. This bridge pattern is helping us to separate our abstraction from its implementation. And how it's going to do that thing. It actually depends that this pattern is going to involve an interface, which is going to act as a bridge between an abstract class and an implemented class. And once this interface is going to act like a bridge is going to give us a great advantage that this particular code is going to be thread safe because of the thread safe environment, we are not going to have any particular thread which is going to access the instance of the code at one time.

Before moving forward to the bridge pattern, just ask this question to yourself that do you actually need a bridge pattern? Now the answer is if you have a project which actually having a code which needs to be changed very oftenly then this is the right pattern for you. And you can manage your code smartly with this bridge pattern. In our code sample, we are going to create one communicated application with the name ping me, this ping me application is actually going to do the communication between different types of applications. And when we are going to do communication, we are going to use some time some ms mq services, some email message services, or maybe some web services. It's a basic communication scenario which we are going to fulfill in this code.

And to do that, we are going to take a help of bridge pattern in my visit, should you have a solution explorer in which we have two projects, one is class library, and the other one is a console application. The first one is the console application, which is having a program class inside the head while this one is a C sharp class. So first, I will delete the C sharp class and then inside this class library, we are going to add one new item Which is actually going to be an interface with the name I message sender, this iMessage sender is going to be public. And inside this, I'm going to specify one new method. With the name send message. You can see that the send message is actually returning void.

And this is having two parameters, which are string, subject and body. It's something like that this interface is just having this method signature. And then we are going to create some classes which can implement this interface. I'm going to right click on this project and I'm going to add a new C sharp class with the name email sender. This email sender class is going to implement that I message sender. So I'm going to put column and I'm going to put a message sender.

I'm going to make sure that this class is going to be public. And then if we implement this interface, we are going to get that method Inside the Send Message method, we are going to write some simple logic. And using these parameters, we are going to certify. That is going to be console dot write line in which we are going to print, email. And then there could be just printing that subject and body, my parameter values. It's very simple implementation of this particular class.

And now let's create some more concrete implementation of this interface. So I'm going to add a new C sharp class and let me give it a name. Ms mq sender. This ms mq sender class is also going to implement the same interface IE message sender is going to be public. And then we are going to implement the interface method inside this, which is just going to have one word change in that then this time we are sending a message using ms mq. Let me add one more concrete implementation like this.

So I'm going to add a new class which is going to be Third concrete implementation, I'm going to give a name Web Service Center. This Web Service Center is also going to be public. And I'm going to implement a method center in this. The only difference in the output is this is going to have a word web service in that once you're done with this, we are going to click on see wall. And you can see in my project I have one interface and three classes which are connected with that one interface. Once you're done with this, let's create our actual important class which is going to be message and that message class is going to be useful in the communication.

So I'm going to right click add new item is going to be a class with the name message. This message class is going to be an abstract class because maybe in message also we are going to have different kind of message which we need to send. So we are going to add a new abstract class within the message. And this message abstract class you can see is having some three properties inside that the first property He's off type I message sender. And this iMessage sender is actually the same interface which we have done. This is something which is a connection between my interface and this abstract class.

Other than this, we have some more properties inside this class, like subject and body. And then we have one method, which is actually send a message. And the Send method is just an abstract method, which is returning nothing. It's very simple abstract class. And because this is an abstract class, we need a concrete implementation of this. So, we're going to right click on this and then go to add a new item, which is going to be a class.

And let's say that the name of the class is going to be system message. The system message class is going to implement that abstract class. So I'm putting column and I'm going to put message this class is also going to be public. And then we're going to implement that abstract class into this Once we implement this abstract class, you can see we are getting a send method. And the Send method is going to have some logic inside that my logic is very simple. We are just using message sender, dot send message.

So we are just simply calling that method send message and we are passing our subject and body do that. Let's add one more new C sharp class and I'm going to give a name of the C sharp class user message. This is another implementation of the abstract class. So this class is also going to be public. And we are going to implement our abstract class which is message there are possibilities that the logic of this particular class is going to be different. So after implementing this, this method call, I want to change to something else.

You can see that inside the Send method, I'm going to write in logic, which is going to be like this. This call we have a new parameter which is actually using Comment and this user comment is nothing but it's going to be a property, which I'm going to declare on the top like this. This is a comment is is actually a property of this particular class. And you can see that using this user comment, we are actually going to create one full body which is a string parameter and this is going to be the full body which are going to pass into my Send Message method. This is a customized logic which is just going to have some extra user comments also into the execution. Once you're done with this, we are going to click on Save all and if you see we have one message class, which is connected with two implementations and that is using message and system message.

While we have one interface, which is a message sender, which is connected with three different implementations of that, do not forget that we have a message class which is having a property method sender, which is having a return type of type interface I message sender and this is what the bridge Which we are talking about. We're going to save this. And now it's the time to implement this into a console application. So first thing, let's add the reference of pygmy dot core into this project. And then once we have a reference, we are going to add an instance on the top using pygmy dot code. Inside this main method, I'm going to write a logic to use those classes.

But first, let me do one thing let me create an object of email sender, Ms mq sender, and web service center classes. And you can see that these three objects are actually a reference type of a message center. So we are actually initializing a three different concrete implementation objects on the top. Once we are done with the three object. Next, I'm going to create an object of a message of system message right now. So this message is actually a new instance of a system message class and using that object, we are accessing the property, subject and body.

And I'm passing some simple string values into that. Now once we have a message, it's time to send a message through one of the message sender, either email, queue or web. To do that, I'm going to create message dot. And if you see we have a method sender. In this we can choose one of the methods sender, and first time choosing email. Then I'm going to do message dot, and then I have a method which is sent.

The moment I call the Send method is going to send a message using email. Same way we can choose some other centers also. So I'm going to choose second time which is going to be Q. And once I select Q, I'm going to again going to do message dot said. Let's try the third one also. So again, they do message dot message sender, and this time we are going to specify web servers So he's going to be lab.

And then we are going to do message dot set. So we are sending the same kind of message, which is a system message, but we are using different different senders. And right now we can do the same because these things are actually connected with our interface type reference property. Done with this web service sender, email sender is actually connected with the help of the system message. And it's totally loosely coupled, because we have that reference type of variable of type interface. So this is a main important part of this bridge.

Now let's try with the user message also. So I'm creating a new object of type, maybe user message. And this user message is going to be this time instance of our user message class. So I'm doing new user message. And then using this user message dot, I'm able to access the properties of that class. So if we do user message dot, you can see I have all the properties body subject into that.

And then we are going to use right now that user message dot subject. And we'll pass some string values into that. So let's say that we are passing equals to. And this is from user. Let's pass the body also. So we are doing user message dot body.

And maybe this is a simple body like. Hi, how's you and that's it. Now, do not forget one thing once we give a subject and a body using this user message class, in the user message class, we have one extra thing we have one extra property which is user comment, which is also part of our full body. We use that use a comment, you should not use an reference variable of type message Actually, this should be of type user message because user message is having that extra property So we have to change the return type of this to user message. And now if I do that, you say message dot, I should get the property, which is user comments. Let's add some extra string comments into that.

That equals to please work. And I hope this will work. So we're just going to put semicolon, and now it's the time to associate this thing with the message sender. So let's do user message dot message sender is equals to let's say, we agree to choose email. And then we are going to use a message dot send, and this should send that thing with the help of email. Up to this point, we are done and let's check this thing by quickly running this project.

This last line which we are going to add is console dot read line so that this is going to wait in the Output Screen. Now let's run this project. You can see I'm adding a console message and you can see this console is showing First we have send the text message using email. And then we are sending a face message with me ms mq. And then we are sending with delco web service. The last one is also an email message, but this is actually using the help of user comment.

So you can see that is having a user message with the extra user comment, which is pleased work at the end. In short, our core is working fine. And we are actually using different different message senders with the help of interface and we are using different types of messages with the help of that extra class, if you understood up to this point. Now, this is totally a loosely cover scenario. If tomorrow, I need to change the logic of any of the methods enter classes, or I need to change the customization of my system message or use a message then that will be totally loosely coupled. And that is a great advantage of a bridge pattern.

Now let's quickly understand the structure of this pattern. As you know that we have one interface With the name I message sender, which is connected with over three concrete implementation classes, email sender, web service sender, an MS mq center, we have one abstract class with the name message which is indirectly connected with a message sender as a property return type. And that's why that connection is dotted. And we have a message asset class which is implemented with two concrete implementations use a message and sister message. This is a simple connection of the bridge pattern. And the bridge between this interface and abstract class is the main important thing, which is making this scenario loosely coupled.

I hope you understood this. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.