Who in this video we're going to talk about the last design pattern in this course.
The question 42 What is the observer design pattern?
Observer design pattern allows objects to notify other objects about changes in their state.
Let's consider the following example We have some glass that is able to read the current bitcoin price
in real life application.
It would read from some public API, but for example, sake, let's make it return a random number between
zero and 50 thousands.
Now, let's say we want to create a couple of mechanisms that will notify the applications users if
the price has grown over a certain threshold.
Let's say we want to be able to send users emails or push notifications.
The class sending the push notifications will be almost the same, except that the message will be different.
This is, of course, a simplification, and in a real project, those classes would actually send emails
or push notifications.
We could also implement more classes to perform other types of notifications.
All right.
So here is the big picture we have the bitcoin price reader that reads the price and two classes that
wait to be notified about the price turned email.
Price terms notifier and pousse price terms notifier when the price is read from the Bitcoin price reader,
we want to execute the update method from both the classes that wait for the information about the new
price.
Well, this is awkward, at least, first of all, this court tightly captures the bitcoin price free
there with the other two classes.
Secondly, this way will only notify a single email price to notify the object and the single push price
them to notify the object.
What if we want to notify our whole group of them?
Lastly, what if some of those objects will no longer be interested in listening about the price changes?
Now we do not have any control over what objects we notify.
It's time to introduce the observer design pattern.
Let's do it step by step.
First of all, we want to decouple the bitcoin price reader.
So the opposite are available from the price trend to notify us so the observers will need to define
interfaces over which they can communicate.
The first question we need to ask is what data will be sent from the observable to the observers.
In our case, it will be the current bitcoin price.
So this email?
But let's make the interfaces generic so they can work with any payload.
First, let's define the eye observer interface, which will be implemented by the price tag and notify
us.
This interface will contain a single update method, which will be called by the observer variable to
send the data to the observer's.
Let's use this interface before we move on to our observable.
In this case, the update method is already implemented, so not much to do here in general.
The update method is the one that receives the notification from the observable and decides what to
do about it.
Now let's define the observable interface.
The first two methods are used to attach or subscribe the observer to the observable.
This way, we'll have control over who is notified.
We can detach or unsubscribe the observer at any time if there are no longer interested in receiving
the notifications from the observer boom.
This method will be executed to send the notification to our subscribed observers.
Let's implement this interface in the Bitcoin price with across.
We'll start by defining a collection of observers.
As you can see, those methods simply add or remove the element from the observer's east.
The Notify Observers method will simply iterate this list of observers and execute the update method
on them.
The only thing left to do is to call the notify observers method after the latest bitcoin price has
been read.
Let's see again what happened.
The price change father's classes now implement the I eye observer interface.
This interface forces them to have the update method, which takes the current bitcoin price.
Those observers can be added to the observers list owned by the observable.
In our case, the bitcoin price reader, they are added or removed from this list using those two methods.
When the observer who wants to notify the observers, it simply reiterates this list and executes the
update method.
In our case, it happens when the latest bitcoin price is right.
Let's put it all together.
First, let's create the observers and attach them to the observable.
Let's say the email should be sent to the user if the price of bitcoin exceeds 25 thousands and push
notification when it exceeds 40 thousands.
Let's execute the route current price method a couple of times.
Let's see what will be the result of this program.
All right.
So the current bitcoin price is something above 31 thousands.
So we send an email, but the price isn't high enough to send a push notification.
Let's run the application again.
This time, the threshold of 40000 was exceeded, so both notifications have been sent as expected.
Now let's say that some of our users got tired with receiving emails and he only wants to get the push
notifications.
That means we must detach the email price and not fire from the observable.
And now, as you can see, after the Imus notification has been turned off, only the push notification
has been sent.
It means the observer was successfully detached.
Remember that this code bases on random numbers.
So when you execute it, you will have different results, run it a couple of times and see what happens.
All right.
We implemented the basic observer designed button.
Please note that there is an existing Microsoft's implementation of this pattern, but it's a bit more
complex.
I wanted to show you a custom implementation.
So you see exactly what is going on.
If you are curious about Microsoft's implementation, check out the resources attached to this lecture.
We will revisit the topic of the observer design pattern in the next lecture, where we'll talk about
events as they have very much in common when discussing this pattern during the interview.
You should be ready for the question in the observer design pattern.
What is the observable and what is the observer?
The observer who is the object that's being observed by observers?
The observable notifies the observers about a change in each state.
All right.
Thanks for following along.
In the topic of these old patterns, I see you in the next lecture.