Introduction to Mediator Design Pattern

A free video tutorial from Paulo Dichone | Software Engineer, AWS Cloud Practitioner & Instructor
Android, Flutter, AWS, Best Selling Instructor
Rating: 4.5 out of 5Instructor rating
23 courses
236,943 students
Introduction to Mediator Design Pattern

Learn more from the full course

Java Design Patterns: The Complete Masterclass - 21 Patterns

Master Java Design Patterns and Write Robust and Extensible Software - MVC, Observable, Singleton, Facade, Builder...

09:50:47 of on-demand video • Updated February 2020

Master Java Design Patterns
Apply Design Patterns to Real-World Projects
Build Software that's Robust and Flexible Using Java Design Patterns
Solve common Software architecture problems with Java Design Patterns
English [Auto]
So in this video, we're going to talk about the mediator design pattern to understand the mediator design patterns again. What are we going to do here? Let's look at a complex system of any kind. So who talk about a system that means we have smaller systems or subsystems inside of the bigger system of the complex system that putting it all together. We then have that complex system and obviously to have a complex system that also means these subsystems, they need to communicate with each other. Right. All the subsystems are inside the complex system, have to communicate with each other as such. Now, as you can see, this can get a little bit complicated, especially if we were asked to emulate this complex system in code in Java, code in code, this wouldn't quite work very well. So the solution would be we need to have a centralized communication. What this means is we all still have our complex system. OK, and we'll still have our subsystems as such. However, the difference now is that as opposed to have these subsystems communicating with each other, for instance, Subsystem A could communicate to the middle the subsystem B, which communicates to some subsystem C and vice versa. So that means then they all have to know each other. And on top of that, they also have to be part of the complex system. So that is not ideal, especially as the complexity grows of the system. So the better way would have to centralize that communication right in this case will have to top the complex system, which still contains our SAP systems. But now you notice, what are we going to do is we are going to just have the complex system, the main one, communicating directly to a hub or in this case will be an intermediary class intermediary hub, which will then communicate directly to all of this system, which also means that we are now decoupling the system in such a way that this system doesn't really have to know about each other. Right out of communication now is centralized in one place, and this one place will know how to delegate all that communication for all the systems we have decoupled parts of our system. So what we just discussed this called the mediator design pattern. So we have, again, the complex system and inside we have this subsystems and now what are we going to do? Of course, as I said, we are communicating directly. Each subsystem communicates directly with the hub, in this case, the intermediary or the mediator, which knows what to do. OK, and all of these have systems now in the lingo of the mediator, they are called colleagues. The subsystems are our colleagues. Now, the media design pattern, an example that I can give you here. Imagine what happens at the air traffic controller. So we have the main hub, which is the air traffic controller, and then we have airplanes. So these are going to be the colleagues. So the idea here is that each airplane doesn't really know about other airplanes. All they know is that they communicate directly to the ATC, the air traffic controller, which then knows exactly how to communicate with each individual airplane. So now we can have a million airplanes. In fact, this is how it works, right? Because at the airport there is a million while there's a lot of airplanes come in and out and they don't really know about each other, they just know how to communicate to the air traffic controller, which knows then how to delegate to each of the colleagues in this case, each of the airplanes to say, OK, Airplane eight is now ready to go off an airplane. We can now descend and all sorts of things, all the logistics, it's all in one place. So this is one of the examples that I think helps us understand the mediator design pattern. Again, those are all colleagues. Now, the mediator design pattern, really, the definition is centralizes, complex communications and control between related objects, again, it makes sure that all of the communication is centralized in one place. In this one place, the mediator in this case will know how to then communicate with other objects so that these objects, his colleagues are not having to know of each other and they just have to know how to communicate and receive information, vice versa, from them to the mediator. OK, so that is the gist of it. And pretty much that's really what a mediator design pattern really does. OK, so let's go ahead and take a look.