Heads up! To view this whole video, sign in with your Courses Plus 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 concepts behind the Observer Design Pattern. The implementation in Java has evolved with the language, let's explore some use cases.
Pre-requisites
- Introduction to Design Patterns
- Learn Java track or a good grasp on the basics of the Java language and JDK.
Definition
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Code
- The Java Observer Patterns repository on GitHub.
[SOUND] Hello I'm Craig and
I'm a developer.
0:00
In this workshop we're gonna explore
the extremely popular design pattern,
0:06
Observer.
0:10
We'll explore what makes the pattern so
popular and
0:11
we'll also walk through the why,
the how, and the when you should use it.
0:13
First though,
0:18
just want to remind you that there
are speed controls on the video player.
0:19
Make use of those to speed me up or
slow me down to your tastes.
0:22
Always remember to check
the teacher's notes on each video.
0:25
I've drop some prerequisites on this one.
0:28
So take a moment and
0:30
make sure that we're on the same
page before we get started here.
0:30
And finally, don't forget to hit up
the community to discuss these ideas more.
0:33
I'm excited to take the time to walk
through the Observer pattern with you.
0:37
It's a very powerful and
commonly used pattern.
0:41
It's ability to make code more
extensible is what makes it so popular.
0:43
So you'll see it used all
over the place as a solution.
0:47
Now, I guarantee that you've
interacted with this pattern already.
0:49
It' everywhere.
0:52
I mean, you're literally
seeing it at play right now.
0:54
See the timeline on this video?
0:56
The toolbar is observing
where you are in this video.
0:58
As you progress through this video,
it changes, right?
1:02
It updates both the number of seconds as
well as the rendering of the progress bar.
1:05
So in the video example, the video
itself represents the master object, or
1:10
subject as it's often referred to.
1:14
It's the one portion of the one-to-many.
1:17
The many side of things, the dependent
objects or observers as they're usually
1:20
called, those are the number of seconds
displayed and the progress bar.
1:24
So the subject changes its state and
1:28
its observers are notified and
update themselves accordingly.
1:31
So with that in mind,
you can probably think
1:35
of a couple other examples where you can
interact with this pattern at work today.
1:37
Now, for instance,
I was just using a spreadsheet.
1:41
When I updated the cell with my percentage
complete on a project that I'm working on,
1:44
the graph changed.
1:47
And so did several other fields
that were observing that value.
1:48
Those other objects were notified
by the changes I made in my cells.
1:52
Now the Observer pattern is at play
in the compound design pattern MVC.
1:55
The V in MVC, or view layer,
makes heavy use of this pattern.
1:59
The view observes the model.
2:04
Changes to the model will notify the view.
2:06
Now UI libraries like the ones that
you've interacted with, like JavaFX or
2:09
Android apps, rely heavily on this pattern
to display the current state of the model.
2:12
Now, this pattern is often referred
to as Pub/Sub which is short for
2:17
Publish/Subscribe.
2:21
Now this might help things stick.
2:23
A newspaper is published and delivered
to all of it's subscribers, right?
2:24
And that makes sense.
2:28
You get the paper because
you have a subscription, and
2:29
it only comes when a new
issue is published.
2:31
All right, all right, enough analogies.
2:34
So let's go take a quick
look at the UML so
2:36
you can see the suggested implementation.
2:38
So if we do a quick search for
the Observer pattern,
2:41
we'll see here that the first
result here is from Wikipedia.
2:44
So let's click this.
2:49
Let's scroll down to the UML.
2:50
I'm going to go ahead
I'm going to click this.
2:51
It's gonna open it up and
I'm gonna zoom in on it.
2:52
So we can take a more detailed look here.
2:55
Okay, so we see that there's
an Observer interface, and
2:57
it has a method called notify.
3:00
There's several concrete
implementations here, right?
3:03
So Concrete A and B, and they also
implement, have a method called notify.
3:06
So there's a little diamond
here on a class called subject,
3:11
and what that means is there's
an aggregation of these observers here and
3:15
they're being stored in
this observer collection.
3:19
And they get added by calling
a thing called registerObserver, and
3:20
you pass in the observer or
unregister to remove it.
3:24
And then there is a method
called notifyObservers, and
3:26
this is the pseudo code here.
3:29
Basically it says for each one of
these observers, loop through and
3:31
call the notify method.
3:34
So basically what this is
saying is that any object that
3:36
implements the observer
interface can be notified.
3:39
So, now don't worry, you'll get your
head wrapped around it here in a bit.
3:43
Just remember that this is a suggestion
of how to implement the pattern.
3:46
There are numerous ways of
achieving this pattern in code and
3:50
we'll hit them up
separately here in a bit.
3:52
Now these names aren't required,
but they are common.
3:56
So you can start to recognize
them when you see them.
3:58
While we're here, why don't we go get the
starter files for application installed?
4:00
So in the teacher's notes
there's a link to a rebo.
4:05
I went ahead and
copied that and pasted it.
4:07
Here we are on GitHub.
4:09
I'm gonna choose clone or download,
I'm gonna grab the URL here for SSH, and
4:11
I'm gonna pop over to my InteliJ,
which you'll see is running 2016.2.1.
4:17
And I'm gonna check out from
version control, use GitHub,
4:23
I'm gonna put in the password for this.
4:28
So I'm gonna come here and
I'm going to paste in.
4:35
And want to choose clone.
4:39
And it says would you like to open?
4:44
I'm going to say yes.
4:45
It says it's an unlinked Gradle project.
4:47
I'm gonna say go ahead and
import the Gradle project.
4:48
And I'm gonna click it again.
4:50
And just click all the defaults here,
okay.
4:53
Okay, so I've built this was
several different modules where
5:07
we can break up the different use
cases that we're going to do.
5:10
So go ahead and just leave all of
those checked by default and click OK.
5:13
When you take a look here under Project,
you can see that the different ones here
5:20
are in bold are the modules that
we'll be looking at here just a bit.
5:24
So now that we've seen it have
a pretty basic understanding,
5:28
what do you say we put it into practice?
5:30
This pattern and
its usage has evolved along with Java.
5:32
I think a good approach is to walk
through a few of the iterations
5:36
since you'll encounter
them in your journeys.
5:39
We'll discuss the pros and
cons of each approach.
5:41
Let's start with
the original implementation.
5:44
It's been around since version 1.1.
5:46
So let's dust it off and
dive into it next.
5:48
You need to sign up for Treehouse in order to download course files.
Sign up