1

00:00:00.06 --> 00:00:01.04

- [Instructor] So now that we've



2

00:00:01.04 --> 00:00:03.00

talked about Factory pattern,



3

00:00:03.00 --> 00:00:05.04

it's time to see it in action.



4

00:00:05.04 --> 00:00:08.00

If you open up the exercise files in your IDE,



5

00:00:08.00 --> 00:00:11.03

you'll see that I've already started the application for you



6

00:00:11.03 --> 00:00:12.08

by creating a Spring Boot starter



7

00:00:12.08 --> 00:00:16.00

and adding a couple of controller class



8

00:00:16.00 --> 00:00:18.00

as well as an interface



9

00:00:18.00 --> 00:00:21.00

and some implementations for our Factory.



10

00:00:21.00 --> 00:00:23.03

Let's take a look at the interface real quick.



11

00:00:23.03 --> 00:00:26.02

You'll see my Pet interface has several methods.



12

00:00:26.02 --> 00:00:27.02

Things that make sense



13

00:00:27.02 --> 00:00:29.02

when dealing with various types of pets



14

00:00:29.02 --> 00:00:32.08

that are really equal across all pets.



15

00:00:32.08 --> 00:00:35.02

And that's really how we want to start



16

00:00:35.02 --> 00:00:36.08

when dealing with the Factory.



17

00:00:36.08 --> 00:00:40.08

So we're going to create a new class called PetFactory.



18

00:00:40.08 --> 00:00:44.01

And this Factory we're going to want Spring to actually manage.



19

00:00:44.01 --> 00:00:47.05

So we're going to do an @Component annotation on it.



20

00:00:47.05 --> 00:00:51.05

Now there's going to be one method on our Factory.



21

00:00:51.05 --> 00:00:54.04

And that method is the Factory method itself.



22

00:00:54.04 --> 00:00:56.09

So it's going to return a Pet



23

00:00:56.09 --> 00:00:59.09

and it's going to be called createPet



24

00:00:59.09 --> 00:01:04.01

and it's going to take a string of animalType.



25

00:01:04.01 --> 00:01:07.00

Now we could put some null checks and things in here.



26

00:01:07.00 --> 00:01:09.05

But we're not going to do that for the purpose of brevity.



27

00:01:09.05 --> 00:01:12.07

We'll just use good programming practices in the real world



28

00:01:12.07 --> 00:01:15.06

as far as checking the bounds.



29

00:01:15.06 --> 00:01:17.09

So we're going to go switch



30

00:01:17.09 --> 00:01:23.01

and we're going to do a switch on animalType.toLowerCase.



31

00:01:23.01 --> 00:01:24.01

And this is so we can make sure



32

00:01:24.01 --> 00:01:25.02

that everything that comes in



33

00:01:25.02 --> 00:01:27.06

we can actually do the switch on.



34

00:01:27.06 --> 00:01:32.05

So the case that we're going to do at first is dog.



35

00:01:32.05 --> 00:01:34.03

And when it comes in as a dog,



36

00:01:34.03 --> 00:01:39.04

we'll return a new dog.



37

00:01:39.04 --> 00:01:43.05

And then we're going to put in a case of cat



38

00:01:43.05 --> 00:01:46.09

and we will return a new cat.



39

00:01:46.09 --> 00:01:49.08

Now obviously we can have a lot more animals at play.



40

00:01:49.08 --> 00:01:52.01

But this is all that I've created for you.



41

00:01:52.01 --> 00:01:54.05

So we'll do a default



42

00:01:54.05 --> 00:01:55.07

and in the default case



43

00:01:55.07 --> 00:02:01.03

we will throw a new UnsupportedOPerationException



44

00:02:01.03 --> 00:02:04.02

saying unknown animal type.



45

00:02:04.02 --> 00:02:06.05

And it's always good to have this default case



46

00:02:06.05 --> 00:02:09.02

in a switch statement as well as in a Factory



47

00:02:09.02 --> 00:02:10.05

to make sure that



48

00:02:10.05 --> 00:02:12.05

if someone passes in something you can't handle,



49

00:02:12.05 --> 00:02:14.07

you have an out.



50

00:02:14.07 --> 00:02:18.00

So now what I'd like you to do is open up the controller



51

00:02:18.00 --> 00:02:21.06

and the AppController itself.



52

00:02:21.06 --> 00:02:23.08

We're going to add a few things to this.



53

00:02:23.08 --> 00:02:24.07

First thing we're going to do



54

00:02:24.07 --> 00:02:29.02

is we're going to autowire an instance of the PetFactory.



55

00:02:29.02 --> 00:02:31.03

Now if you've see any of my videos before,



56

00:02:31.03 --> 00:02:32.09

you know that I'm going to tell you



57

00:02:32.09 --> 00:02:35.01

not to do what I just did.



58

00:02:35.01 --> 00:02:37.01

We're not focusing on Spring framework,



59

00:02:37.01 --> 00:02:39.02

so I'm just going to kind of be quick and dirty here.



60

00:02:39.02 --> 00:02:40.03

In all honesty,



61

00:02:40.03 --> 00:02:42.01

we should've put that in a constructor



62

00:02:42.01 --> 00:02:44.02

and autowired in a constructor



63

00:02:44.02 --> 00:02:45.06

but for the purpose of brevity



64

00:02:45.06 --> 00:02:47.06

this will work just fine.



65

00:02:47.06 --> 00:02:51.01

So we're going to do a post mapping method



66

00:02:51.01 --> 00:02:55.07

and we're going to do adoptPet



67

00:02:55.07 --> 00:02:59.04

and we're going to pass it a parameter of type



68

00:02:59.04 --> 00:03:02.04

and then we're going to pass it a parameter of name



69

00:03:02.04 --> 00:03:04.00

because we want you to name your pet



70

00:03:04.00 --> 00:03:06.09

before we allow you to adopt it.



71

00:03:06.09 --> 00:03:12.07

So we're going to do Pet adoptPet.



72

00:03:12.07 --> 00:03:17.01

Go ahead and import that.



73

00:03:17.01 --> 00:03:21.04

And now to this we're going to send in a PathVariable



74

00:03:21.04 --> 00:03:27.03

and the PathVariable is a string called type



75

00:03:27.03 --> 00:03:30.04

and then we're going to do another PathVariable



76

00:03:30.04 --> 00:03:33.01

that is a string of name.



77

00:03:33.01 --> 00:03:38.02

So now we can actually see the power of the Factory itself.



78

00:03:38.02 --> 00:03:43.06

We will create a new Pet from our Factory



79

00:03:43.06 --> 00:03:45.05

and we don't need to know anything about



80

00:03:45.05 --> 00:03:48.05

what we are doing when we call create.



81

00:03:48.05 --> 00:03:51.04

We just need to know that we're going to create a pet.



82

00:03:51.04 --> 00:03:55.06

We're going to allow the user to tell us what types to create.



83

00:03:55.06 --> 00:03:59.01

But we also know that every time we get a pet



84

00:03:59.01 --> 00:04:00.01

that we're going to adopt,



85

00:04:00.01 --> 00:04:02.03

we're going to set the name.



86

00:04:02.03 --> 00:04:03.07

This is undisputed.



87

00:04:03.07 --> 00:04:04.05

It doesn't matter



88

00:04:04.05 --> 00:04:08.00

if it's a dog or a cat or a bird or a snake,



89

00:04:08.00 --> 00:04:11.04

when you adopt it we're going to set the name.



90

00:04:11.04 --> 00:04:13.07

We're also going to be good stewards



91

00:04:13.07 --> 00:04:18.03

and we're going to feed the pet before we let you take it home.



92

00:04:18.03 --> 00:04:19.02

And finally,



93

00:04:19.02 --> 00:04:21.07

after we've got a name and we fed it,



94

00:04:21.07 --> 00:04:26.04

then and only then can you have the pet back.



95

00:04:26.04 --> 00:04:29.07

And this is where the Factory method comes into play.



96

00:04:29.07 --> 00:04:32.02

Because we created it without any knowledge



97

00:04:32.02 --> 00:04:33.08

of what we we're creating.



98

00:04:33.08 --> 00:04:36.00

But we knew that there were rules we we're going to follow.



99

00:04:36.00 --> 00:04:37.05

We we're going to name it and feed it



100

00:04:37.05 --> 00:04:39.06

before we ever let you have it,



101

00:04:39.06 --> 00:04:41.03

and that's how the Factory pattern



102

00:04:41.03 --> 00:04:46.01

applies this common logic to an interface



103

00:04:46.01 --> 00:04:47.05

so that we can construct the object,



104

00:04:47.05 --> 00:04:48.09

provide the logic,



105

00:04:48.09 --> 00:04:50.05

and then do work.



106

00:04:50.05 --> 00:04:52.07

This is a very powerful pattern.



107

00:04:52.07 --> 00:04:54.04

Let's go ahead and start up our application,



108

00:04:54.04 --> 00:04:57.07

if you're not already running it



109

00:04:57.07 --> 00:04:59.00

and as this starts



110

00:04:59.00 --> 00:05:00.09

I'm going to jump over to my terminal.



111

00:05:00.09 --> 00:05:04.09

Now I use a tool called httpie.



112

00:05:04.09 --> 00:05:08.02

You can do the same thing with curl if you want.



113

00:05:08.02 --> 00:05:14.02

But I'm going to post to local host 8080 adoptPet.



114

00:05:14.02 --> 00:05:18.06

We're going to get a dog and we're going to name him Rover.



115

00:05:18.06 --> 00:05:22.00

And we get the response out that we would expect.



116

00:05:22.00 --> 00:05:28.08

Let's change this to cat and let's call her Lucy.



117

00:05:28.08 --> 00:05:30.07

And you'll see, as we would expect,



118

00:05:30.07 --> 00:05:33.08

both times they're not hungry, both times they're named.



119

00:05:33.08 --> 00:05:36.00

But we have different animals.



120

00:05:36.00 --> 00:05:37.09

And that's the Factory pattern.



