1

00:00:00.05 --> 00:00:02.00

- [Instructor] The Mediator pattern is another one



2

00:00:02.00 --> 00:00:04.02

of these patterns that we see used in a couple places



3

00:00:04.02 --> 00:00:06.04

in the Spring Framework, but we don't use



4

00:00:06.04 --> 00:00:08.07

that often when we write code.



5

00:00:08.07 --> 00:00:11.01

In the Spring Framework, the single most important



6

00:00:11.01 --> 00:00:13.02

place that we see the Mediator pattern used is



7

00:00:13.02 --> 00:00:15.09

in the DispatcherServlet and the way



8

00:00:15.09 --> 00:00:18.06

that it operates in Spring MVC.



9

00:00:18.06 --> 00:00:20.00

Now, often it can also be used



10

00:00:20.00 --> 00:00:23.02

when setting the state of various components at once,



11

00:00:23.02 --> 00:00:24.06

and that's another time that we see



12

00:00:24.06 --> 00:00:26.04

that used throughout the Framework



13

00:00:26.04 --> 00:00:28.02

because when they have to do this,



14

00:00:28.02 --> 00:00:31.00

these are all hidden kind of underneath the covers.



15

00:00:31.00 --> 00:00:34.01

So how does this pattern actually work?



16

00:00:34.01 --> 00:00:36.07

So, the Mediator provides encapsulation



17

00:00:36.07 --> 00:00:39.01

on object behavior.



18

00:00:39.01 --> 00:00:42.02

Communication channels flow through the Mediator,



19

00:00:42.02 --> 00:00:45.00

who then, in turn, allow its mediated classes



20

00:00:45.00 --> 00:00:46.08

to do work.



21

00:00:46.08 --> 00:00:49.07

Mediated classes do not communicate with each other



22

00:00:49.07 --> 00:00:52.02

and that's really the key of this pattern.



23

00:00:52.02 --> 00:00:55.04

They only communicate with the Mediator.



24

00:00:55.04 --> 00:00:57.04

And the Mediator classes themselves



25

00:00:57.04 --> 00:01:01.01

can register or unregister as needed.



26

00:01:01.01 --> 00:01:02.07

So why would we want to go about



27

00:01:02.07 --> 00:01:05.07

using the Mediator pattern?



28

00:01:05.07 --> 00:01:07.02

One of the biggest uses is we want to



29

00:01:07.02 --> 00:01:10.07

reduce the exponential growth that occurs



30

00:01:10.07 --> 00:01:13.05

when you have interobject communication.



31

00:01:13.05 --> 00:01:15.04

When you have two objects that talk to each other,



32

00:01:15.04 --> 00:01:17.05

you have one communication path.



33

00:01:17.05 --> 00:01:19.07

When you have three, that jumps.



34

00:01:19.07 --> 00:01:22.00

When you have five, that jumps even more.



35

00:01:22.00 --> 00:01:24.06

And we get this exponential growth curve



36

00:01:24.06 --> 00:01:26.09

as more and more objects have to communicate



37

00:01:26.09 --> 00:01:27.08

with each other.



38

00:01:27.08 --> 00:01:29.06

This is one of the key points



39

00:01:29.06 --> 00:01:33.00

of using a Mediator pattern.



40

00:01:33.00 --> 00:01:35.08

Once again we see this tight coupling come up.



41

00:01:35.08 --> 00:01:37.09

And using the Mediator pattern allows us



42

00:01:37.09 --> 00:01:41.06

to reduce the tight coupling between these objects



43

00:01:41.06 --> 00:01:43.04

that interact.



44

00:01:43.04 --> 00:01:47.01

And finally, it makes interaction flows between objects



45

00:01:47.01 --> 00:01:49.00

more resistant to changes,



46

00:01:49.00 --> 00:01:52.07

because as an object, I only know about the Mediator



47

00:01:52.07 --> 00:01:55.02

as a mediated object, and not the other objects



48

00:01:55.02 --> 00:01:56.09

that I need to communicate with.



49

00:01:56.09 --> 00:01:58.05

I allow the Mediator to do



50

00:01:58.05 --> 00:02:01.08

that communication on my behalf.



51

00:02:01.08 --> 00:02:03.05

So if you wanted to use a Mediator



52

00:02:03.05 --> 00:02:05.05

how would you go about doing it?



53

00:02:05.05 --> 00:02:06.09

First and foremost, you have



54

00:02:06.09 --> 00:02:09.08

to create the mediated interface.



55

00:02:09.08 --> 00:02:13.06

Then you have to create a Mediator interface



56

00:02:13.06 --> 00:02:15.09

with the method to register the mediated,



57

00:02:15.09 --> 00:02:18.06

as well as unregister the mediated,



58

00:02:18.06 --> 00:02:20.07

and then any structured commands that you need



59

00:02:20.07 --> 00:02:24.03

to support through this Mediator flow.



60

00:02:24.03 --> 00:02:26.08

Then you're going to create concrete implementations



61

00:02:26.08 --> 00:02:29.01

of each as needed.



62

00:02:29.01 --> 00:02:31.08

And finally, in these structured commands



63

00:02:31.08 --> 00:02:34.00

you're going to mediate the behavior to all



64

00:02:34.00 --> 00:02:37.02

of the mediated objects, as appropriate.



65

00:02:37.02 --> 00:02:39.06

You're going to allow those objects to take the message



66

00:02:39.06 --> 00:02:41.00

and do what they want with it.



67

00:02:41.00 --> 00:02:43.01

You're just mediating the calls.



68

00:02:43.01 --> 00:02:45.00

You receive a call in, you send it



69

00:02:45.00 --> 00:02:46.09

to every object you mediate.



70

00:02:46.09 --> 00:02:49.02

This is how we would go about doing this.



71

00:02:49.02 --> 00:02:51.09

And these are very special use cases



72

00:02:51.09 --> 00:02:53.07

in these behavior patterns.



73

00:02:53.07 --> 00:02:55.06

And it's good to know about them.



74

00:02:55.06 --> 00:02:57.04

We don't actually see them that often



75

00:02:57.04 --> 00:02:59.03

when we're implementing them, until you get



76

00:02:59.03 --> 00:03:01.09

into really complex systems programming.



77

00:03:01.09 --> 00:03:04.04

The Mediator is really, really useful



78

00:03:04.04 --> 00:03:05.09

when you have lots of things you've got



79

00:03:05.09 --> 00:03:07.07

to talk to at one time,



80

00:03:07.07 --> 00:03:09.01

and you want to prevent them



81

00:03:09.01 --> 00:03:11.00

from talking to each other



82

00:03:11.00 --> 00:03:12.05

And that's why we would choose it.



83

00:03:12.05 --> 00:03:14.07

And this is sort of the basic strategy.



