1 00:00:07,230 --> 00:00:13,740 Who in this video we're going to talk about the last design pattern in this course. 2 00:00:14,070 --> 00:00:18,420 The question 42 What is the observer design pattern? 3 00:00:19,170 --> 00:00:26,520 Observer design pattern allows objects to notify other objects about changes in their state. 4 00:00:27,000 --> 00:00:33,750 Let's consider the following example We have some glass that is able to read the current bitcoin price 5 00:00:34,200 --> 00:00:36,150 in real life application. 6 00:00:36,300 --> 00:00:43,110 It would read from some public API, but for example, sake, let's make it return a random number between 7 00:00:43,110 --> 00:00:45,000 zero and 50 thousands. 8 00:00:45,670 --> 00:00:52,710 Now, let's say we want to create a couple of mechanisms that will notify the applications users if 9 00:00:52,710 --> 00:00:55,860 the price has grown over a certain threshold. 10 00:00:56,250 --> 00:01:01,740 Let's say we want to be able to send users emails or push notifications. 11 00:01:33,550 --> 00:01:40,420 The class sending the push notifications will be almost the same, except that the message will be different. 12 00:01:48,500 --> 00:01:55,220 This is, of course, a simplification, and in a real project, those classes would actually send emails 13 00:01:55,220 --> 00:01:56,930 or push notifications. 14 00:01:57,350 --> 00:02:02,570 We could also implement more classes to perform other types of notifications. 15 00:02:04,360 --> 00:02:04,890 All right. 16 00:02:05,020 --> 00:02:12,550 So here is the big picture we have the bitcoin price reader that reads the price and two classes that 17 00:02:12,550 --> 00:02:16,360 wait to be notified about the price turned email. 18 00:02:16,390 --> 00:02:23,890 Price terms notifier and pousse price terms notifier when the price is read from the Bitcoin price reader, 19 00:02:24,100 --> 00:02:30,640 we want to execute the update method from both the classes that wait for the information about the new 20 00:02:30,640 --> 00:02:31,210 price. 21 00:02:58,350 --> 00:03:05,670 Well, this is awkward, at least, first of all, this court tightly captures the bitcoin price free 22 00:03:05,670 --> 00:03:07,800 there with the other two classes. 23 00:03:08,340 --> 00:03:16,710 Secondly, this way will only notify a single email price to notify the object and the single push price 24 00:03:16,830 --> 00:03:18,240 them to notify the object. 25 00:03:18,690 --> 00:03:22,200 What if we want to notify our whole group of them? 26 00:03:22,770 --> 00:03:29,430 Lastly, what if some of those objects will no longer be interested in listening about the price changes? 27 00:03:29,760 --> 00:03:34,200 Now we do not have any control over what objects we notify. 28 00:03:34,650 --> 00:03:38,280 It's time to introduce the observer design pattern. 29 00:03:38,790 --> 00:03:40,530 Let's do it step by step. 30 00:03:41,070 --> 00:03:45,180 First of all, we want to decouple the bitcoin price reader. 31 00:03:45,450 --> 00:03:53,190 So the opposite are available from the price trend to notify us so the observers will need to define 32 00:03:53,190 --> 00:03:56,280 interfaces over which they can communicate. 33 00:03:56,880 --> 00:04:03,970 The first question we need to ask is what data will be sent from the observable to the observers. 34 00:04:04,650 --> 00:04:07,890 In our case, it will be the current bitcoin price. 35 00:04:08,010 --> 00:04:08,970 So this email? 36 00:04:09,180 --> 00:04:13,890 But let's make the interfaces generic so they can work with any payload. 37 00:04:14,400 --> 00:04:20,820 First, let's define the eye observer interface, which will be implemented by the price tag and notify 38 00:04:20,820 --> 00:04:21,180 us. 39 00:04:21,540 --> 00:04:27,570 This interface will contain a single update method, which will be called by the observer variable to 40 00:04:27,570 --> 00:04:29,790 send the data to the observer's. 41 00:04:40,160 --> 00:04:44,030 Let's use this interface before we move on to our observable. 42 00:04:50,610 --> 00:04:56,890 In this case, the update method is already implemented, so not much to do here in general. 43 00:04:57,000 --> 00:05:04,020 The update method is the one that receives the notification from the observable and decides what to 44 00:05:04,020 --> 00:05:05,040 do about it. 45 00:05:05,760 --> 00:05:09,090 Now let's define the observable interface. 46 00:05:29,180 --> 00:05:35,840 The first two methods are used to attach or subscribe the observer to the observable. 47 00:05:36,380 --> 00:05:39,980 This way, we'll have control over who is notified. 48 00:05:40,370 --> 00:05:47,540 We can detach or unsubscribe the observer at any time if there are no longer interested in receiving 49 00:05:47,540 --> 00:05:50,090 the notifications from the observer boom. 50 00:05:50,660 --> 00:05:56,570 This method will be executed to send the notification to our subscribed observers. 51 00:05:57,020 --> 00:06:01,310 Let's implement this interface in the Bitcoin price with across. 52 00:06:01,850 --> 00:06:05,390 We'll start by defining a collection of observers. 53 00:06:31,540 --> 00:06:37,450 As you can see, those methods simply add or remove the element from the observer's east. 54 00:06:38,290 --> 00:06:45,880 The Notify Observers method will simply iterate this list of observers and execute the update method 55 00:06:45,880 --> 00:06:46,420 on them. 56 00:06:58,240 --> 00:07:05,980 The only thing left to do is to call the notify observers method after the latest bitcoin price has 57 00:07:05,980 --> 00:07:06,580 been read. 58 00:07:11,140 --> 00:07:12,820 Let's see again what happened. 59 00:07:13,780 --> 00:07:19,330 The price change father's classes now implement the I eye observer interface. 60 00:07:20,050 --> 00:07:26,020 This interface forces them to have the update method, which takes the current bitcoin price. 61 00:07:26,470 --> 00:07:32,300 Those observers can be added to the observers list owned by the observable. 62 00:07:32,710 --> 00:07:40,420 In our case, the bitcoin price reader, they are added or removed from this list using those two methods. 63 00:07:41,050 --> 00:07:47,830 When the observer who wants to notify the observers, it simply reiterates this list and executes the 64 00:07:47,830 --> 00:07:48,880 update method. 65 00:07:49,480 --> 00:07:53,560 In our case, it happens when the latest bitcoin price is right. 66 00:07:54,400 --> 00:07:55,750 Let's put it all together. 67 00:07:56,020 --> 00:08:00,490 First, let's create the observers and attach them to the observable. 68 00:08:01,270 --> 00:08:08,290 Let's say the email should be sent to the user if the price of bitcoin exceeds 25 thousands and push 69 00:08:08,290 --> 00:08:11,440 notification when it exceeds 40 thousands. 70 00:08:34,290 --> 00:08:38,030 Let's execute the route current price method a couple of times. 71 00:08:47,300 --> 00:08:50,000 Let's see what will be the result of this program. 72 00:08:52,360 --> 00:08:53,100 All right. 73 00:08:53,500 --> 00:08:58,030 So the current bitcoin price is something above 31 thousands. 74 00:08:58,510 --> 00:09:04,210 So we send an email, but the price isn't high enough to send a push notification. 75 00:09:04,660 --> 00:09:06,190 Let's run the application again. 76 00:09:09,030 --> 00:09:16,260 This time, the threshold of 40000 was exceeded, so both notifications have been sent as expected. 77 00:09:17,080 --> 00:09:24,720 Now let's say that some of our users got tired with receiving emails and he only wants to get the push 78 00:09:24,720 --> 00:09:25,740 notifications. 79 00:09:26,250 --> 00:09:31,410 That means we must detach the email price and not fire from the observable. 80 00:09:43,070 --> 00:09:50,420 And now, as you can see, after the Imus notification has been turned off, only the push notification 81 00:09:50,420 --> 00:09:51,380 has been sent. 82 00:09:51,650 --> 00:09:54,680 It means the observer was successfully detached. 83 00:09:55,940 --> 00:09:59,570 Remember that this code bases on random numbers. 84 00:09:59,780 --> 00:10:06,860 So when you execute it, you will have different results, run it a couple of times and see what happens. 85 00:10:07,370 --> 00:10:08,090 All right. 86 00:10:08,270 --> 00:10:11,810 We implemented the basic observer designed button. 87 00:10:12,170 --> 00:10:18,710 Please note that there is an existing Microsoft's implementation of this pattern, but it's a bit more 88 00:10:18,710 --> 00:10:19,400 complex. 89 00:10:19,940 --> 00:10:22,580 I wanted to show you a custom implementation. 90 00:10:22,850 --> 00:10:25,250 So you see exactly what is going on. 91 00:10:25,730 --> 00:10:32,150 If you are curious about Microsoft's implementation, check out the resources attached to this lecture. 92 00:10:32,600 --> 00:10:38,750 We will revisit the topic of the observer design pattern in the next lecture, where we'll talk about 93 00:10:38,750 --> 00:10:45,050 events as they have very much in common when discussing this pattern during the interview. 94 00:10:45,110 --> 00:10:49,280 You should be ready for the question in the observer design pattern. 95 00:10:49,550 --> 00:10:53,150 What is the observable and what is the observer? 96 00:10:53,720 --> 00:10:58,700 The observer who is the object that's being observed by observers? 97 00:10:59,240 --> 00:11:04,430 The observable notifies the observers about a change in each state. 98 00:11:05,210 --> 00:11:05,990 All right. 99 00:11:06,560 --> 00:11:08,120 Thanks for following along. 100 00:11:08,120 --> 00:11:12,680 In the topic of these old patterns, I see you in the next lecture.