1

00:00:00.02 --> 00:00:01.08

- [Instructor] So now that we've talked a little bit



2

00:00:01.08 --> 00:00:04.00

about the prototype pattern I want to show you



3

00:00:04.00 --> 00:00:06.01

how it's used in the Spring Framework



4

00:00:06.01 --> 00:00:08.09

as opposed to the Java Framework this time.



5

00:00:08.09 --> 00:00:11.06

So let's jump into our IDE



6

00:00:11.06 --> 00:00:13.09

and just like we've done a couple times before



7

00:00:13.09 --> 00:00:19.06

let's create a new package in our base package



8

00:00:19.06 --> 00:00:26.07

And this time we're going to call it Prototype.



9

00:00:26.07 --> 00:00:30.00

Now we're going to create two very dumb classes here



10

00:00:30.00 --> 00:00:35.03

and the first we're going to call ProtoTrue



11

00:00:35.03 --> 00:00:38.05

and the second



12

00:00:38.05 --> 00:00:43.00

we're going to call ProtoFalse



13

00:00:43.00 --> 00:00:44.08

Now we're not going to do anything with either one



14

00:00:44.08 --> 00:00:48.00

of these classes as far as implementation.



15

00:00:48.00 --> 00:00:49.05

What we are going to do is jump into



16

00:00:49.05 --> 00:00:53.00

the Design Patterns App application.



17

00:00:53.00 --> 00:00:58.02

Alright, so in this class we're going to create a Bean.



18

00:00:58.02 --> 00:01:01.07

Public ProtoFalse



19

00:01:01.07 --> 00:01:04.05

and we'll simply call it that



20

00:01:04.05 --> 00:01:10.08

and we will return a new ProtoFalse object.



21

00:01:10.08 --> 00:01:13.01

Now we're going to do another Bean



22

00:01:13.01 --> 00:01:16.04

and this time we're going to set it Scope



23

00:01:16.04 --> 00:01:19.08

to be equal to



24

00:01:19.08 --> 00:01:23.09

prototype.



25

00:01:23.09 --> 00:01:32.05

Now we're going to do a public ProtoTrue



26

00:01:32.05 --> 00:01:37.03

and we will return a new ProtoTrue.



27

00:01:37.03 --> 00:01:39.08

Now, in Spring this is all we have to do



28

00:01:39.08 --> 00:01:41.09

to make our Bean a Prototype.



29

00:01:41.09 --> 00:01:44.01

So as we're looking at this what I would expect



30

00:01:44.01 --> 00:01:47.01

to see is if we do a test



31

00:01:47.01 --> 00:01:49.07

two instances of ProtoFalse will be identical



32

00:01:49.07 --> 00:01:53.06

whereas two instances of ProtoTrue will not be.



33

00:01:53.06 --> 00:01:55.00

And let's jump into our test



34

00:01:55.00 --> 00:01:57.05

and actually see if that's the case.



35

00:01:57.05 --> 00:01:58.08

So we're going to jump down here



36

00:01:58.08 --> 00:02:02.03

into our Design Patterns application once again



37

00:02:02.03 --> 00:02:08.07

and just like we did before we will auto wire in



38

00:02:08.07 --> 00:02:11.00

an instance of ProtoTrue



39

00:02:11.00 --> 00:02:14.09

and we will call it true1.



40

00:02:14.09 --> 00:02:19.03

And auto wire in an instance of ProtoTrue



41

00:02:19.03 --> 00:02:24.07

and we will call it true2.



42

00:02:24.07 --> 00:02:30.02

And now we're going to do the same for ProtoFalse.



43

00:02:30.02 --> 00:02:35.09

False one and at Auto Wired



44

00:02:35.09 --> 00:02:38.06

on ProtoFalse



45

00:02:38.06 --> 00:02:40.03

false2.



46

00:02:40.03 --> 00:02:43.00

Now we will create our test.



47

00:02:43.00 --> 00:02:48.09

Public void testPrototypes.



48

00:02:48.09 --> 00:02:52.02

Alright, so we are going to say



49

00:02:52.02 --> 00:02:57.06

Assert.assertSame



50

00:02:57.06 --> 00:03:01.02

on false1 and false2.



51

00:03:01.02 --> 00:03:02.09

So what we're saying is that those two objects



52

00:03:02.09 --> 00:03:04.03

are going to be the exact same



53

00:03:04.03 --> 00:03:06.03

the same object reference.



54

00:03:06.03 --> 00:03:12.01

At the same time, we're going to say assert



55

00:03:12.01 --> 00:03:14.08

not same



56

00:03:14.08 --> 00:03:17.04

on true1



57

00:03:17.04 --> 00:03:18.02

and true2.



58

00:03:18.02 --> 00:03:19.09

And I know you got to kind of think about that



59

00:03:19.09 --> 00:03:21.08

'cause it's a little backwards



60

00:03:21.08 --> 00:03:25.00

but what that's saying is that the Prototype False objects



61

00:03:25.00 --> 00:03:27.07

the ones that we did not set to be Prototype



62

00:03:27.07 --> 00:03:29.06

therefore singleton are going to be the same



63

00:03:29.06 --> 00:03:32.01

as we saw in our singleton test.



64

00:03:32.01 --> 00:03:35.00

We're also saying that the two beans



65

00:03:35.00 --> 00:03:38.08

that are prototype beans will not be the same.



66

00:03:38.08 --> 00:03:42.08

So now let's go ahead and run our test



67

00:03:42.08 --> 00:03:45.04

And as Spring starts up we'll let it do its thing



68

00:03:45.04 --> 00:03:49.09

and we expect to see a green test at this case.



69

00:03:49.09 --> 00:03:51.07

And indeed we have a green test.



70

00:03:51.07 --> 00:03:54.07

So we've proven that we now have two different instances



71

00:03:54.07 --> 00:03:56.03

of the same object.



72

00:03:56.03 --> 00:03:59.01

Now let's go back to the App application



73

00:03:59.01 --> 00:04:01.01

for the Design Patterns here



74

00:04:01.01 --> 00:04:03.08

and let's talk about what this actually would entail



75

00:04:03.08 --> 00:04:06.00

on line 25.



76

00:04:06.00 --> 00:04:09.04

Very seldom would we simply return an instance.



77

00:04:09.04 --> 00:04:12.08

What we're going to do in Spring usually during this method



78

00:04:12.08 --> 00:04:15.08

is at all of the expansive behavior



79

00:04:15.08 --> 00:04:20.00

through injecting other beans into this class.



80

00:04:20.00 --> 00:04:21.05

And once it's all injected



81

00:04:21.05 --> 00:04:23.08

then we're going to hand it back out.



82

00:04:23.08 --> 00:04:26.01

Now if all of those values are beans that means



83

00:04:26.01 --> 00:04:28.04

that they're singletons and they've been controlled



84

00:04:28.04 --> 00:04:31.06

and we can actually put behavior in the class



85

00:04:31.06 --> 00:04:33.08

that doesn't have to worry about being thread-safe



86

00:04:33.08 --> 00:04:36.09

while keeping all of those singletons in place.



87

00:04:36.09 --> 00:04:38.04

So in the real world



88

00:04:38.04 --> 00:04:41.04

all of your work is done in this method



89

00:04:41.04 --> 00:04:43.01

but because it's a prototype



90

00:04:43.01 --> 00:04:46.02

the expansive operations that get injected



91

00:04:46.02 --> 00:04:48.01

into this class have already been done.



92

00:04:48.01 --> 00:04:49.09

It's only the work that goes on here



93

00:04:49.09 --> 00:04:53.01

so that you get a bean that acts as a prototype.



94

00:04:53.01 --> 00:04:55.08

It acts as a cloned object even though



95

00:04:55.08 --> 00:04:58.02

it really isn't in the Spring world.



