1

00:00:00.05 --> 00:00:01.09

- [Instructor] Now, the first creational pattern



2

00:00:01.09 --> 00:00:05.02

that we're going to talk about is the factory pattern.



3

00:00:05.02 --> 00:00:07.01

And this is probably, of all of the patterns,



4

00:00:07.01 --> 00:00:11.03

the one that most developers have actually seen and used.



5

00:00:11.03 --> 00:00:13.04

I've used this pattern thousands of times



6

00:00:13.04 --> 00:00:16.01

throughout my professional career.



7

00:00:16.01 --> 00:00:18.06

Now, in Spring, the pattern is used



8

00:00:18.06 --> 00:00:20.03

throughout the entire framework.



9

00:00:20.03 --> 00:00:24.02

But one of the primary places it's used is the BeanFactory.



10

00:00:24.02 --> 00:00:26.01

And this is a little bit of a modification



11

00:00:26.01 --> 00:00:28.02

of a traditional factory pattern.



12

00:00:28.02 --> 00:00:32.02

But the BeanFactory itself is indeed a factory.



13

00:00:32.02 --> 00:00:34.05

In addition to the BeanFactory,



14

00:00:34.05 --> 00:00:37.01

there is a FactoryBean that goes with it that



15

00:00:37.01 --> 00:00:40.00

gives you this concept of a factory of factories,



16

00:00:40.00 --> 00:00:42.08

so to speak, if you look at how they work.



17

00:00:42.08 --> 00:00:45.01

Now, the reason I mentioned that these are so



18

00:00:45.01 --> 00:00:48.05

cored to Spring is because the IoC container



19

00:00:48.05 --> 00:00:51.00

is based on the application context.



20

00:00:51.00 --> 00:00:54.06

The application context extends the BeanFactory.



21

00:00:54.06 --> 00:00:56.01

And actually keeps a handle to it,



22

00:00:56.01 --> 00:00:59.08

because it acts as a wrapper around the BeanFactory.



23

00:00:59.08 --> 00:01:01.08

This is where all of your configured object



24

00:01:01.08 --> 00:01:05.07

references are stored in the Spring Framework.



25

00:01:05.07 --> 00:01:08.01

Again, it's leveraged heavily in the framework,



26

00:01:08.01 --> 00:01:09.08

but this is one of the big ones that most



27

00:01:09.08 --> 00:01:13.05

developers touch when they first start with Spring.



28

00:01:13.05 --> 00:01:15.03

Now, in a nutshell, the way that the



29

00:01:15.03 --> 00:01:18.04

pattern works is it allows for construction



30

00:01:18.04 --> 00:01:21.07

of similar classes of different types,



31

00:01:21.07 --> 00:01:24.02

using a so-called factory method.



32

00:01:24.02 --> 00:01:25.08

Now, what does that mean?



33

00:01:25.08 --> 00:01:27.09

You have a method call that creates the



34

00:01:27.09 --> 00:01:31.06

object for you and serves it back to you.



35

00:01:31.06 --> 00:01:34.06

Those objects are constructed from classes



36

00:01:34.06 --> 00:01:38.07

that share an interface or a parent class.



37

00:01:38.07 --> 00:01:41.07

This shared behavior of these classes



38

00:01:41.07 --> 00:01:44.01

allows the factory to reduce



39

00:01:44.01 --> 00:01:46.07

the amount of coupling in your code.



40

00:01:46.07 --> 00:01:48.06

So, why would we use these?



41

00:01:48.06 --> 00:01:49.08

It really, it comes down to,



42

00:01:49.08 --> 00:01:52.07

it allows you to not worry about class



43

00:01:52.07 --> 00:01:56.01

construction in more than one place.



44

00:01:56.01 --> 00:01:57.04

It allows you to leverage the



45

00:01:57.04 --> 00:02:00.08

interface for repetitive operations.



46

00:02:00.08 --> 00:02:05.04

And, the lack of construction code in your code



47

00:02:05.04 --> 00:02:08.09

makes copy and paste errors less likely.



48

00:02:08.09 --> 00:02:10.08

Now, the creation strategy that we're



49

00:02:10.08 --> 00:02:13.08

going to come up with for writing a factory



50

00:02:13.08 --> 00:02:17.04

is always code to an interface, if possible.



51

00:02:17.04 --> 00:02:19.06

This is a Gang of Four construct.



52

00:02:19.06 --> 00:02:20.06

But more so, it's good



53

00:02:20.06 --> 00:02:23.05

object-oriented programming practices.



54

00:02:23.05 --> 00:02:25.04

And when you're working with a factory,



55

00:02:25.04 --> 00:02:28.09

the real benefit comes from coding to the interface,



56

00:02:28.09 --> 00:02:32.01

instead of the concrete implementation.



57

00:02:32.01 --> 00:02:34.06

So, you're going to create that common interface



58

00:02:34.06 --> 00:02:38.02

that you're going to use in your factory objects.



59

00:02:38.02 --> 00:02:39.09

Then, you're going to create a class



60

00:02:39.09 --> 00:02:44.08

that gives instances of the interface.



61

00:02:44.08 --> 00:02:47.09

And in that class, you're going to implement



62

00:02:47.09 --> 00:02:52.01

a method that serves those concrete classes



63

00:02:52.01 --> 00:02:56.01

that you will then implement from the interface itself.



64

00:02:56.01 --> 00:02:58.03

So, again, we have a class that has a



65

00:02:58.03 --> 00:03:02.06

method that creates instances of the interface



66

00:03:02.06 --> 00:03:04.01

when under the coverage that it's actually



67

00:03:04.01 --> 00:03:06.05

creating instances in the concrete class.



68

00:03:06.05 --> 00:03:08.06

Then it serves back out.



69

00:03:08.06 --> 00:03:10.01

So, the consuming code,



70

00:03:10.01 --> 00:03:12.03

the code that consumes the factory,



71

00:03:12.03 --> 00:03:15.03

will create an interface and operate on that interface,



72

00:03:15.03 --> 00:03:17.00

even though the factory itself has



73

00:03:17.00 --> 00:03:20.09

served a concrete implementation.



74

00:03:20.09 --> 00:03:23.03

Now, there's a sister of pattern



75

00:03:23.03 --> 00:03:25.04

that goes with the factory pattern.



76

00:03:25.04 --> 00:03:28.05

And that is the Abstract Factory Pattern.



77

00:03:28.05 --> 00:03:31.05

And this really takes it one step further.



78

00:03:31.05 --> 00:03:33.02

And what do I mean by that?



79

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

It becomes a factory of factories.



80

00:03:36.03 --> 00:03:38.05

So, you create the base factory.



81

00:03:38.05 --> 00:03:40.03

But all of your factories didn't share



82

00:03:40.03 --> 00:03:44.00

a common interface that the parent abstract



83

00:03:44.00 --> 00:03:48.06

factory will use to create the sub-factory,



84

00:03:48.06 --> 00:03:51.01

and then the instance object from it.



85

00:03:51.01 --> 00:03:53.02

It really gets complicated when you start to



86

00:03:53.02 --> 00:03:55.09

look at it until you've used it a few times.



87

00:03:55.09 --> 00:03:57.07

Because you have these various layers of



88

00:03:57.07 --> 00:04:00.08

abstraction that you've got to fight through.



89

00:04:00.08 --> 00:04:02.08

And this new layer of abstraction



90

00:04:02.08 --> 00:04:05.04

becomes very powerful if you have a bunch



91

00:04:05.04 --> 00:04:08.08

of types that also have a bunch of types.



92

00:04:08.08 --> 00:04:10.09

So, think about that for a minute.



93

00:04:10.09 --> 00:04:12.02

If we look at the high level,



94

00:04:12.02 --> 00:04:15.01

so let's look at the case where we have a bunch of pets.



95

00:04:15.01 --> 00:04:18.02

Those pets are all animals, different types of animals.



96

00:04:18.02 --> 00:04:19.04

And within those animals,



97

00:04:19.04 --> 00:04:22.01

there's different breeds and different sizes.



98

00:04:22.01 --> 00:04:23.09

And different feeding behaviors.



99

00:04:23.09 --> 00:04:26.08

But all in all, you have a factory of



100

00:04:26.08 --> 00:04:29.05

a factory that creates instance objects.



101

00:04:29.05 --> 00:04:32.08

And that's what this Abstract Factory Pattern is.



102

00:04:32.08 --> 00:04:34.02

Now, we're not going to go too deep in that.



103

00:04:34.02 --> 00:04:36.06

We're going to stick with the parent factory pattern.



104

00:04:36.06 --> 00:04:38.09

And let's jump into that in some code next.



