1

00:00:00.07 --> 00:00:02.07

[Instructor] The Adapter Pattern is one of those patterns



2

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

that really takes a moment to wrap your mind around



3

00:00:05.07 --> 00:00:08.03

and it becomes even harder to find objects that fit in



4

00:00:08.03 --> 00:00:11.06

to this pattern when you try to create them at will



5

00:00:11.06 --> 00:00:14.09

because we tend to think of things that are similar,



6

00:00:14.09 --> 00:00:18.01

but very similar, not loosely similar.



7

00:00:18.01 --> 00:00:20.01

And the Adapter Pattern plays very well



8

00:00:20.01 --> 00:00:22.05

in that loosely similar set.



9

00:00:22.05 --> 00:00:24.09

Let's talk a little bit about how the Adapter Pattern



10

00:00:24.09 --> 00:00:26.04

is used in Spring.



11

00:00:26.04 --> 00:00:28.09

One if the most common use case or the one



12

00:00:28.09 --> 00:00:32.09

that I've dealt with the most is with Spring integration.



13

00:00:32.09 --> 00:00:36.08

Spring integration actually leverages Channel Adapters



14

00:00:36.08 --> 00:00:39.08

throughout the entire operation for communication



15

00:00:39.08 --> 00:00:41.05

with different systems.



16

00:00:41.05 --> 00:00:45.08

It allows you to build a channel and within that channel



17

00:00:45.08 --> 00:00:49.03

that have different communication in points without having



18

00:00:49.03 --> 00:00:51.09

to manipulate the behavior of the channel.



19

00:00:51.09 --> 00:00:54.02

So in Spring integration this is a great example



20

00:00:54.02 --> 00:00:58.01

of very similar but not that similar objects



21

00:00:58.01 --> 00:01:00.04

having shared methodology.



22

00:01:00.04 --> 00:01:03.09

It's also used a lot with the internal operations of AspectJ



23

00:01:03.09 --> 00:01:07.03

specifically during loading of the application context



24

00:01:07.03 --> 00:01:09.07

and the weaving that takes place.



25

00:01:09.07 --> 00:01:11.06

Now we're going to spend most of this time talking about



26

00:01:11.06 --> 00:01:14.08

the pattern as it exist in Java and not necessarily



27

00:01:14.08 --> 00:01:17.00

how it exist in Spring.



28

00:01:17.00 --> 00:01:19.02

Now, the way that the pattern works is that you



29

00:01:19.02 --> 00:01:22.04

have two different interfaces or two different objects



30

00:01:22.04 --> 00:01:26.01

that share a very common operation.



31

00:01:26.01 --> 00:01:29.09

Not identical always but it's a very common operation.



32

00:01:29.09 --> 00:01:33.06

Now an adapter is a wrapper class that is created



33

00:01:33.06 --> 00:01:37.02

and it holds an instance of one of the interfaces



34

00:01:37.02 --> 00:01:39.09

while implementing the other interface



35

00:01:39.09 --> 00:01:44.00

and that's how it adapts the two interfaces together.



36

00:01:44.00 --> 00:01:46.08

And specifically, that shared operation



37

00:01:46.08 --> 00:01:50.03

of the wrapper interface is implemented in such as way



38

00:01:50.03 --> 00:01:54.04

that it calls the similarly shared operation



39

00:01:54.04 --> 00:01:56.06

of the interface that is wrapped.



40

00:01:56.06 --> 00:01:58.07

Hence, adapting the two.



41

00:01:58.07 --> 00:02:01.07

That common path gives ton in the adapter



42

00:02:01.07 --> 00:02:05.06

as opposed to trying to manipulate the interfaces themselves



43

00:02:05.06 --> 00:02:07.05

to be something that they're not.



44

00:02:07.05 --> 00:02:10.06

And often that's really where the power comes in.



45

00:02:10.06 --> 00:02:14.03

Instead of trying to make things fit, you can use adapters



46

00:02:14.03 --> 00:02:16.03

in certain code flows.



47

00:02:16.03 --> 00:02:18.05

So why would we want to do this?



48

00:02:18.05 --> 00:02:20.00

One of the most common use cases



49

00:02:20.00 --> 00:02:23.01

that I originally was introduced to with this pattern



50

00:02:23.01 --> 00:02:26.05

was when you were working with legacy or third party code,



51

00:02:26.05 --> 00:02:29.01

that you need it to fit into your code.



52

00:02:29.01 --> 00:02:31.07

Often, we didn't want to manipulate the legacy code



53

00:02:31.07 --> 00:02:34.04

because of all the bugs that might introduce



54

00:02:34.04 --> 00:02:36.08

or we didn't have the ability to manipulate



55

00:02:36.08 --> 00:02:39.04

third party code so by leveraging adapters



56

00:02:39.04 --> 00:02:44.00

we could use their code in our way that we needed to.



57

00:02:44.00 --> 00:02:46.09

It also allows you to share functionality



58

00:02:46.09 --> 00:02:50.05

on very disparate objects to reduce code.



59

00:02:50.05 --> 00:02:53.00

So let's say that you have widgets and sprockets



60

00:02:53.00 --> 00:02:56.03

and for all intends and purposes they are very different



61

00:02:56.03 --> 00:02:59.01

until they run down the factory assembly line.



62

00:02:59.01 --> 00:03:01.00

When they're coming down the assembly line,



63

00:03:01.00 --> 00:03:03.08

they act very much the exact same



64

00:03:03.08 --> 00:03:06.03

and that's where the adapters comes into play.



65

00:03:06.03 --> 00:03:09.08

The adapter can treat those two objects as the same



66

00:03:09.08 --> 00:03:12.06

during that path of code where they would be



67

00:03:12.06 --> 00:03:14.05

on the assembly line.



68

00:03:14.05 --> 00:03:18.02

And this will allow you to code two-way simple interface



69

00:03:18.02 --> 00:03:21.00

in this routine workflow so that assembly line



70

00:03:21.00 --> 00:03:24.00

is the routine workflow and everything that happens on it



71

00:03:24.00 --> 00:03:27.06

we would code into the interface of the adapter.



72

00:03:27.06 --> 00:03:30.02

And by doing this, we can reduce the code



73

00:03:30.02 --> 00:03:33.02

which of course improves readability.



74

00:03:33.02 --> 00:03:36.03

So how would we go about creating this?



75

00:03:36.03 --> 00:03:40.04

We're going to start with two interfaces that share a method.



76

00:03:40.04 --> 00:03:43.00

Now that method doesn't have to be exact.



77

00:03:43.00 --> 00:03:45.08

And really it only needs to be logically similar.



78

00:03:45.08 --> 00:03:48.00

There's no requirements on name being the same



79

00:03:48.00 --> 00:03:50.07

or anything like that, though, often in examples



80

00:03:50.07 --> 00:03:53.04

you will see the name being the exact same.



81

00:03:53.04 --> 00:03:55.08

That's only for example purposes.



82

00:03:55.08 --> 00:03:59.00

The key here is that logically they behave



83

00:03:59.00 --> 00:04:02.01

in enough similar fashion that you can treat them



84

00:04:02.01 --> 00:04:05.03

as the same in a certain block of code.



85

00:04:05.03 --> 00:04:06.09

So once you have those two interfaces,



86

00:04:06.09 --> 00:04:08.04

you're going to implement both of those



87

00:04:08.04 --> 00:04:10.02

with your concrete classes.



88

00:04:10.02 --> 00:04:12.04

You may have more than one concrete class



89

00:04:12.04 --> 00:04:15.03

but at least need one in order to make this work.



90

00:04:15.03 --> 00:04:17.03

Then you're going to create your wrapper class



91

00:04:17.03 --> 00:04:20.00

and this wrapper class is the adapter.



92

00:04:20.00 --> 00:04:21.09

You're going to pick one of the two interfaces



93

00:04:21.09 --> 00:04:24.00

and usually it's the one that is more close



94

00:04:24.00 --> 00:04:25.07

to what you're writing.



95

00:04:25.07 --> 00:04:28.08

You're going to pick that one an you're going to implement it.



96

00:04:28.08 --> 00:04:32.08

Then within that wrapper class, you're going to have a handle



97

00:04:32.08 --> 00:04:36.00

to an instance of the other interface.



98

00:04:36.00 --> 00:04:38.01

So what I mean by that is you're going to keep a handle



99

00:04:38.01 --> 00:04:40.07

to the interface that you can then inject



100

00:04:40.07 --> 00:04:43.05

a concrete class into it.



101

00:04:43.05 --> 00:04:47.09

Then the implementation of the adapter itself



102

00:04:47.09 --> 00:04:51.01

will have a method of that interface.



103

00:04:51.01 --> 00:04:55.03

In that method, you call the similar method



104

00:04:55.03 --> 00:04:57.07

on the instance interface.



105

00:04:57.07 --> 00:05:00.05

That way, the behavior comes true with the interface



106

00:05:00.05 --> 00:05:04.01

that we're exposing but ultimately the underlying interface



107

00:05:04.01 --> 00:05:08.03

of the adapted class is what it is executed.



108

00:05:08.03 --> 00:05:10.09

We're going to jump into this more as we get into code



109

00:05:10.09 --> 00:05:12.08

and you can see what I'm talking about.



