Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's explore the Behavioral 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
Our next category is behavior.
0:00
And it includes design patterns
which determine how objects
0:02
interact with each other and
distribute responsibility.
0:05
It's brought to you today from
the software design principle.
0:08
Identify the aspects of your
application that vary and
0:11
separate them from what stays the same.
0:14
So you take the parts that
constantly change and
0:17
encapsulate them or isolate them
from the parts that don't change.
0:19
This design approach lets you alter or
0:23
extend the code that changes without
affecting other parts of your application.
0:25
These patterns are very
common in frameworks, so
0:29
you'll recognize a bunch of these.
0:32
Now, probably not so much by name,
but by what they do.
0:33
But that said let's take
a look at the grouping.
0:36
[SOUND].
0:38
Here they are.
You'll be surprised how
0:39
much you've interacted
with these patterns.
0:41
Especially observer,
0:44
which you encounter usually during
event handling or iterator for
0:45
that matter which you encounter when you
wanna move through a collection of items.
0:49
Now the strategy pattern here, it allows
you to encapsulate your algorithms.
0:53
The code that does
the nitty gritty details.
0:56
Often times there are many ways to
achieve the same thing in code.
1:00
Some are faster but take more memory and
some are slow but more accurate, but
1:04
the point is this.
1:08
If you wanna be able to
swap out these algorithms,
1:09
strategy is the pattern for you.
1:11
What I wanna do is to take a deeper
look together at the Template Method.
1:13
The template method design pattern
takes the idea of the strategy pattern
1:18
just a little bit farther.
1:21
It allows you to define the basic
1:23
definition of how
an algorithm is laid out.
1:25
But it allows you to
swap out certain parts.
1:27
Let's go take a look.
1:29
I like the layout for the template method
pattern on the source making site.
1:32
This pattern is all over the place,
so there are tons of sources.
1:36
That the Intent section here, describes
how the template pattern will work.
1:39
Now, the template algorithm provides
the basic order of the code.
1:43
And then sub classes, classes that
extend the class through inheritance,
1:47
well they define different methods.
1:51
Now this leaves the original
algorithm in place.
1:53
Though restated,
the parent class or base class,
1:55
defines placeholders
that can be overwritten.
1:59
And the problem here, this describes
why you might wanna use the pattern.
2:04
When you have code that is quite similar.
2:09
But yet, not all the logic can be re-used.
2:10
The template pattern allows you
to share what doesn't change and
2:13
expose ways to alter what does.
2:16
Now this lets the base class call
the methods that have been changed.
2:19
But also call whatever methods
have been overwritten or
2:23
modified through polymorphism.
2:27
Hit the teachers notes for more on that.
2:29
So let's go ahead and let's scroll down
and we'll check out the structure here.
2:31
So here the framework
class is the base class.
2:35
And it has methods defined right, so
2:39
we've got template method stepOne,
stepTwo, stepThree.
2:41
And the open arrowhead here
is showing generalization.
2:45
So ApplicationClassOne
extends the FrameworkClass.
2:49
And it overrides stepTwo.
2:54
So therefore when somebody
calls the template method.
2:57
It's gonna call stepOne, stepTwo and
stepThree, that's what it's saying here.
3:00
It's actually gonna call the stepTwo
of the application class.
3:04
But stepOne and
stepThree from the framework class.
3:07
Right, that makes sense?
3:10
And the same thing is happening over
here for application class too.
3:11
Right, so the template method is called
and it's gonna call stepOne, stepOne and
3:14
then it's gonna call stepTwo, and
stepTwo has been overridden, so
3:18
application stepTwo is gonna run here and
then stepThree is gonna go.
3:21
So the same pattern happens
through the template method, but
3:24
it's using the underlying classes methods.
3:27
Pretty cool right.
3:30
So that's probably a little bit obstruct,
let's keep going.
3:32
All right, so this diagram
here shows that sort algorithm
3:36
is a base class and
its template method is sort.
3:40
And first it processes the array and then
it compares and then it returns the array.
3:45
So what these generalizations
here are doing,
3:51
is they're overwriting the compare method
and it's sort ascending or sort descending
3:53
like alpha numeric probably, all right but
it's going to go A to z or z to A.
3:58
And they just write different methods for
the compare here, all right?
4:02
So when compare is called,
it's choosing what that actually means.
4:07
But the algorithm staying the same.
4:12
First it process it, then it calls the
proper compare and then it returns array.
4:13
That's pretty cool right.
4:20
Now you see these all over the place,
especially in frameworks.
4:21
Now if you swing down all the way to
the bottom here, you will see that there
4:25
are some code examples where you
can look in different languages.
4:28
Now that you've seen this pattern
described, keep an eye out for it.
4:32
Code with the activity life
cycles use this all the times.
4:37
So methods like onStart or onOpen that
you override in your code are almost
4:40
always relying on the template
method design pattern.
4:44
So ready to go check out some more?
4:48
Do your calisthenics and
then come back here and
4:50
choose a few of these to look through.
4:52
And remember, some of these are more
common in different languages.
4:54
Check the teacher's notes for
links and more.
4:57
Next up, let's check out our final
category, structural design patterns.
4:59
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