Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Design Patterns!
You have completed Introduction to Design Patterns!
Preview
Let's explore the Creational design pattern category.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Each one of these design
pattern categories has a clear
0:00
reason for existence.
0:03
Today's category, creational,
0:04
was brought to you by the following
object oriented design principle.
0:06
When we choose to program to the interface
and not the concrete implementation,
0:10
we allow our code to be more extensible.
0:15
The player who follows the proper
protocol can be a part of the game.
0:17
The rule goes on further to state.
0:21
So how do we do that?
0:24
How do we ensure that only create typed
variables that we refer to the interface
0:25
and not the concrete class?
0:29
In loosely typed languages you know
languages where you don't need to specify
0:32
a variable's type like JavaScript or
Python, this isn't really an issue.
0:36
But not so easy in more strongly
typed languages like C# or Jave.
0:40
Luckily for us that question was asked
quite a bit over the evolution of software
0:45
design and solutions from leaders
in our industry began to form.
0:49
The majority of creational
design patterns deal with
0:53
how to allow your code to be extensible
by not coding yourself into a corner.
0:55
These patterns abstract the way
instantiation processes.
1:00
And help to make sure that your
code is independent of how its
1:03
objects are created,
composed, and represented.
1:06
They give you a lot of flexibility in
what gets created, how it gets created,
1:10
who creates it and
when the creation happens.
1:14
So let's take a quick look at all
of the creational design patterns
1:17
from the gang of four
catalog in one place.
1:20
So here they are.
1:23
Now don't feel like you
need to memorize these, but
1:25
you should become familiar with them,
at least by name.
1:28
You are bound to encounter these in
the wild, so understanding that there
1:31
is a place that you can, and should,
go to learn more about them, is key.
1:34
In the teacher's notes I've
linked to each of these patterns.
1:39
Let's walk through one of the most popular
ones of these together for practice.
1:42
Let's take a closer look at the singleton.
1:46
Oftentimes in your applications you'll
wanna make sure that there is one and
1:49
only one instance of a certain class.
1:53
This pattern here not only ensures
that there is only one but
1:56
also a global way of getting to it.
1:59
Okay I'm going to search here for
design pattern catalog.
2:03
And I am going to dive into this
sourcemaking.com site here.
2:10
I like this site quite a bit, it feels
like it's been updated this decade
2:14
which for some reason helps us
think it's more legit, right?
2:18
So let's scroll down here so
they've got their categories.
2:21
Here's the creational design
patterns we just talked about and
2:24
here's our singleton.
2:27
So the intent section here on
the singleton is pretty much
2:29
just what we talked about, right?
2:33
It's used to ensure that one and
only one instance exists but
2:35
the second point here talks about
encapsulating how the class
2:40
is initialized, that is hiding
how the instance is created.
2:43
Now, specifically this pattern promotes
something called lazy initialization.
2:47
Now that is not only should there only
ever be one instance of this call, but
2:52
also that instance should only be created
when it's needed by the application.
2:57
And that makes sense right.
3:02
I mean, sometimes objects are super heavy
or take a very long time to initialize.
3:03
You'll notice the sourcemaking site
displays the pattern details a little bit
3:09
different right?
3:13
It has a section called Discussion.
3:13
And that describes both the positives and
negatives of this pattern.
3:15
Now you should be aware that singletons
are notorious for being difficult to test.
3:19
Now there are other ways of accomplishing
the same goals without using a singleton.
3:24
People do often lean on
singletons before they should and
3:28
it has been given a lot of flak for that.
3:31
It's often referred to as an anti-pattern.
3:34
Now check the teacher's notes for
more on that.
3:37
Even if it is an anti-pattern, believe me,
you'll encounter it in your journeys.
3:39
Developers often use the singleton pattern
because of its emphasis on lazy loading.
3:45
Let's take a look down here
at this class diagram.
3:51
All right so the client knows
about the singleton class.
3:55
And the word static here Indicates
that this is an action that the class,
4:00
or blueprint, can do itself without
requiring an instance, right?
4:07
You don't need to create a new object,
it can do it itself.
4:11
Now sometimes it's called a class method.
4:14
In JavaScript static methods are defined
directly on constructor functions.
4:16
So what happens is you provide
a class method that returns
4:22
the only instance of that class.
4:25
Now in languages where you can control
the initialization of an object,
4:28
like in the constructor, you make sure
that the class cannot be created,
4:32
except through this one method.
4:37
So here's an example of singleton in use,
GlobalResource.
4:40
Now a resource here is something
like a connection to a database.
4:45
And that makes sense right?
4:48
You wouldn't wanna keep creating database
connections you'd only want one.
4:49
So let's take a look at the attribute
section that's up here, right?
4:53
It's this section here.
4:58
This minus sign here
means that it's private.
4:59
Again, not all languages support this but
5:03
some use a pretty obvious
naming structure if it doesn't.
5:06
Like, they usually use an underscore
followed by the attribute name.
5:09
So then in methods like this with the
plus, that's public, they're not hidden.
5:13
So imagine the method getInstance.
5:19
It checks if the instance has been
created and if not it creates it,
5:22
no matter how long that might take and
then it sets the instance.
5:26
Take some time and scroll down here and
read these rules of thumb.
5:31
It goes over some pros and
cons quite well.
5:36
I think you'll catch the drift of
the anti-pattern pretty quickly.
5:39
So in order to get the names
of these design patterns
5:43
lodged into your brain, let's do this.
5:46
Now you may need to get your brain
ready so work with me a bit here.
5:48
If you're feeling a little sluggish right
now, why don't you do a stretch real big
5:51
or do some jumping jacks or
something to get your blood pumping.
5:56
Unless of course you're at a cafe or
on public transportation,
5:59
you might not want to do that,
because that's just weird.
6:02
Go ahead pause me, I'll wait.
6:05
Okay, are you refreshed and
ready to learn?
6:08
in the teachers notes, I've placed
links to all the creational patterns.
6:11
Why don't you attempt to walk
through two of them all by yourself.
6:14
When you're done with that,
come back here,
6:17
and we'll swing through our next category,
behavioral design patterns.
6:19
See you soon.
6:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up