Observer Design Pattern
HTML-код
- Опубликовано: 13 окт 2024
- Get the Code: goo.gl/EYGnQ
Best Design Patterns Book : goo.gl/W0wyie
MY UDEMY COURSES ARE 87.5% OFF TIL July 16th ($9.99)
www.udemy.com/
➡️ Python Data Science Series for $9.99 : Highest Rated & Largest Python Udemy Course + 56 Hrs + 200 Videos + Data Science bit.ly/Master_...
➡️ New C++ Programming Bootcamp Series for $9.99 : Over 23 Hrs + 53 Videos + Quizzes + Graded Assignments + New Videos Every Month bit.ly/C_Cours...
Welcome to my Observer Design Pattern Tutorial. I cover a lot in this tutorial including the Observer pattern, how to use it with threads and much more.
The Observer pattern is a software design pattern in which an object, called the subject (Publisher), maintains a list of its dependents, called observers (Subscribers), and notifies them automatically of any state changes, usually by calling one of their methods.
Learn in One Videos for Every Programming Language
Subscribe to Bookmark them: bit.ly/2FWQZTx
C++ : ruclips.net/video/Rub-JsjMhWY/видео.html
Python : ruclips.net/video/N4mEzFDjqtA/видео.html
Java : ruclips.net/video/n-xAqcBCws4/видео.html
PHP : ruclips.net/video/7TF00hJI78Y/видео.html
MySQL : ruclips.net/video/yPu6qV5byu4/видео.html
JavaScript : ruclips.net/video/fju9ii8YsGs/видео.html
C# : ruclips.net/video/lisiwUZJXqQ/видео.html
HTML5 : ruclips.net/video/kDyJN7qQETA/видео.html
CSS3 : ruclips.net/video/CUxH_rWSI1k/видео.html
JQuery : ruclips.net/video/BWXggB-T1jQ/видео.html
TypeScript : ruclips.net/video/-PR_XqW9JJU/видео.html
ECMAScript : ruclips.net/video/Jakoi0G8lBg/видео.html
Swift : ruclips.net/video/dKaojOZ-az8/видео.html
R : ruclips.net/video/s3FozVfd7q4/видео.html
Haskell : ruclips.net/video/02_H3LjqMr8/видео.html
Handlebars : ruclips.net/video/4HuAnM6b2d8/видео.html
Bootstrap : ruclips.net/video/gqOEoUR5RHg/видео.html
Rust : ruclips.net/video/U1EFgCNLDB8/видео.html
Matlab : ruclips.net/video/NSSTkkKRabI/видео.html
Arduino : ruclips.net/video/QO_Jlz1qpDw/видео.html
Crystal : ruclips.net/video/DxFP-Wjqtsc/видео.html
Emacs : ruclips.net/video/Iagbv974GlQ/видео.html
Clojure : ruclips.net/video/ciGyHkDuPAE/видео.html
Shell : ruclips.net/video/hwrnmQumtPw/видео.html
Perl : ruclips.net/video/WEghIXs8F6c/видео.html
Perl6 : ruclips.net/video/l0zPwhgWTgM/видео.html
Elixir : ruclips.net/video/pBNOavRoNL0/видео.html
D : ruclips.net/video/rwZFTnf9bDU/видео.html
Fortran : ruclips.net/video/__2UgFNYgf8/видео.html
LaTeX : ruclips.net/video/VhmkLrOjLsw/видео.html
F# : ruclips.net/video/c7eNDJN758U/видео.html
Kotlin : ruclips.net/video/H_oGi8uuDpA/видео.html
Erlang : ruclips.net/video/IEhwc2q1zG4/видео.html
Groovy : ruclips.net/video/B98jc8hdu9g/видео.html
Scala : ruclips.net/video/DzFt0YkZo8M/видео.html
Lua : ruclips.net/video/iMacxZQMPXs/видео.html
Ruby : ruclips.net/video/Dji9ALCgfpM/видео.html
Go : ruclips.net/video/CF9S4QZuV30/видео.html
Objective C : ruclips.net/video/5esQqZIJ83g/видео.html
Prolog : ruclips.net/video/SykxWpFwMGs/видео.html
LISP : ruclips.net/video/ymSq4wHrqyU/видео.html
Express : ruclips.net/video/xDCKcNBFsuI/видео.html
Jade : ruclips.net/video/l5AXcXAP4r8/видео.html
Sass : ruclips.net/video/wz3kElLbEHE/видео.html
Might I add.. gasp.
7 years later and this content is still making a huge difference! Thank you, Derek!
10 years later and still making a huge difference.
11 years and it still makes a difference
12 years, and it's still as good
@@israelperezmasle6673 13
Best programming tutorial series on the web. Derek, you helped me get my first job out of college. The knowledge I knew that got me my job I learned from your tutorials... not school (which I payed $$$ for).
Rob L I'm very happy to hear that I could help that much :) Thank you for taking the time to tell me. Messages like this are the reason I keep making videos.
Derek Banas I totally agree with Rob. Whenever I use or recognize some of the design patterns which I've learned through your video series, I immediately remember you and I'm feeling thankful. I'm also recommending these tutorials to everyone who needs help understanding design patterns.
Greetings from Zagreb ,Croatia!
+Derek Banas Yeah these tutorials are amazing Derek. These and your algorithms tutorials are basically the equivalent of a computer science course at a prestigious university.
I am at a prestigous University and i am still watching this, top :)
TheBondilein Can you confirm what The Humanity said? :)
7:57 "It's public not pubic" lol I cracked there.
That is all I could ever ask for :) I'm just providing a service that I wish I had years ago. Thankfully the internet (RUclips) allows me to do what I do
Yes indeed. You have to register the observers so you can easily alert them to changes
Thank you :) I love covering the niche topics everyone else ignores. I'm glad you liked it!
Thank you :) Yes I agree. I wish I would have shown both. I was just doing my best to explain everything as simple as possible.
I'm an electrical engineer with a background in hardware design and low level device driver and systems programming, mostly in ARM assembly and C. I was recently tasked with writing a much higher level OO project in C++ and have found that your tutorials are the only ones that thoroughly explain design patterns and other higher level software concepts. Thank you for these videos, they've helped me no end with my job.
I'm working my way through this series and I found this particular tutorial difficult to follow because the UML didn't match the actual code. Even for this small project it really helps to have a matching UML(it has a some 'implements' and 'has a' relationships). Once I updated the UML I rewatched the video and was able to follow along just fine.
You're very welcome. AOP is implemented through the decorator pattern which I cover. I hope it helps
I'm watching these 7 years later and still this tutorial is the best one on Observer pattern. Thank you Derek
Thank you for the nice compliment :)
One more thing, I noticed a small mistake, just wanted to let you know, you already updated the price by doing
price = Double valueOf(..).... You know which line I'm talking about and then when you just wanted to print out the price change at the end, you printed out
price + randNum
Which resulted the randNum being added in the price twice when you are printing it out and it doesn't match with prices set. I was just wondering why the numbers are not matching and found this bug. Thank you Derek.
You could do that. The design patterns aren't set in stone. There are many variations of them, so always feel free to do whatever makes your code most understandable
I'm using a pseudo UML notation. I did this tutorial before I made my UML tutorial so I wanted to make sure people understood what I was doing. Sorry if that was confusing. If I could go back, I would have made the UML tutorial before this one
I'm extremely happy to hear that I was able to help :)
Derek, thank you for these videos. I used to watch your videos long ago before I started school. Now your design pattern videos are now helping me study for a final exam!
You can store the observers in any way. You just need to maintain a list in one way or another. The patterns are just a guide. You can manipulate them in any way that works for your specific program
a slight improvement to register, unregister if i may, use an extra map String, Integer to store indexes, ArrayList index of complexity is N, map's get is 1. you can also use this map to send private messages by accessing indexes in the iterable quickly.
Derek, i have a exam tomorrow about design patterns, and your channel/videos saved my life. Thank you so much man. You're make magic with your videos! Hope my channel about android help videos get cool like yours one day! Salutes from Brazil/Argentina!
14:08 The usage of observer pattern with threads is usually intended to allow subscribing/registering on a different thread from the one running the observer code i.e ensuring observer and subject/publisher don't run on the same thread. This wasn't demonstrated here (The threads were used for concurrency but both observer and subject execute in the same thread) and that confused me for some time. However the video still helped me in other ways.
I'm very happy to have been of help with this topic that I really enjoyed covering :)
Thank you :) I'm very happy you're enjoying the videos. I would have never thought a design patterns tutorial would be this popular
These videos really helped me quick revise design patterns before interviews. Even when i play at 2x speed i can clearly understand what Derek is saying. All examples are fun and easy to remember. Thanks for such amazing tutorial.
I'm very happy that they helped :)
That is a very good point. Thank you for the input. I mainly write these programs out of my head and that is the reason why my naming isn't always perfect. I'm constantly trying to get better :)
I just wanted to mention that I still draw insight from your work after all these years. I code for a living but in my spare time I'm writing a video game. I wanted levers in the game (of whatever shape or form) to notify their observers such that any lever could trigger any number of devices in the game and for multiple levers to be able to trigger the same device. Sure enough, out came the Observer Pattern and it did just the job :) I continue to refer people to your superb tutorials!
That’s awesome :) Thank you for taking the time to tell me! Sometimes I feel like I’m talking to myself in my basement, but messages like yours keep me going
@@derekbanas My pleasure ^_^ thank you again!
Thank you :) I definitely don't hear that very often. Ill do an Eclipse shortcut tutorial as soon as the address book app is finished. I have been wanting to do that. Thanks for the request
Just found your channel and liking it so far!
A fun variation of this pattern is to do a Pull instead of a Push to the observers. For instance, say if every observer had a time or resource intensive operation every time a value is updated. You may not care about all the changes that happen, such as a daily report. At the end of the day, you want the final stock price of all stocks that had movement. You would send a flag to all of the observers that a change occurred. Then, each observer can call back up to get the current value of what was changed on its own timetable. After retrieving the value, it resets the flag and waits for the next day to check again. Quick example, AAPL and GOOG price changes 100 times an a day, and the observers for those 2 stocks are sent a stockChanged flag. End of day, iterate over all your observers and check the flag. AAPL and GOOG come back true, so call back to get the price, add it to the report, and set the flag back to false. IBM didn’t have a price change, so it is skipped.
Not sure if you ever cover this variation in a different video, but it’s pretty neat!
Thank you :) I do my best to cover topics that nobody has even if they will never get many views. The social network tutorial wasn't made by me, but I was asked to give a shout out for it. I'm normally willing to do things like that. Just click the green link in the video to be sent there. I hope it works for you, but I haven't looked at its progress in over a month
I don't use design patterns at the beginning of the coding process, but instead add them in increments when I add features or guess that features are needed in the future. I'll be showing my process in my code refactoring tutorial which will start tomorrow. I also cover the process in my finished object oriented design tutorial. I hope they help
Yes that is one of the things it is commonly used for
Merry Christmas and Happy New Year to you :) I'm going to make a Design Pattern cheat sheet so they are easier to pick. I'll be covering a bunch more aside from those covered by the GOF.
Hello Derek, I am an experienced java developer who from time to time needs to refresh his core knowledge. Thank you very much for your work. your videos really help me see things in a better and easy way. You really clear some doubts I had before... Thank you mate.
+davicho12345 Hello, Thank you :) I'm very happy that you find the videos useful.
You're very welcome :) It is my privilege to make them. It is very nice to help people
Java is a fabulous language that is made perfect when combined with c / c++. PHP is good for what it does, but it definitely is not an oop language like people keep trying to tell me :)
Yes the Observer pattern is used heavily in the Swing framework.
To be honest I hated being a stock broker. I actually quit after I became a chartered financial analyst. I was more then likely in a bad office, but my experience was so terrible I left the industry. One reason why I left was my office / the home office lost me a $4 Million muni account because they wouldn't print up the portfolio! I love being in business for myself. I don't rely on anyone and I still get to play around with investing
Thank you :) I do my best to make everything understandable even if I have to divert from normal teaching styles
Not many people know about me. I'm glad you found me and enjoy the videos :)
Thank you very much :) I'm trying to provide a complete software engineering degree online, but I still have a way to go. I'll keep at it
Thank you :) Android pretty much uses MVC already. The interface is setup completely using the xml layout files. The Activity files act as the controllers. I code do a better job of modeling the Model better and separating everything better. Thank you for the input
I welcome criticism and you are indeed correct. Sometimes, or even most of the time it is hard to provide a perfect example that both explains the pattern and is easy to understand. That may say something about my own personal skills. I need to work harder to make better tutorials and I try to improve with each video.
In the GetTheStock class, its better to use: `stock.equals("IBM")` than `stock == "IBM"`.
Also in the StockGrabber class' unregister method, it makes more sense to print the "deleted" message after the observer is removed.
You're very welcome :) I have a java tutorial. Technically this is a java tutorial. I haven't covered JEE, but I definitely will. I hope to cover everything there is to know about java
2022 and is, by far, the best tutuorial to learn about design pattern.
great video.
In a long time, I thought the class for observer and the class for subject resides on different machines, but after reading tutorials and seeing codes, I find they are implemented together and run on one same machine.
You would want to definitely save this information to a database. I'm not sure if you are looking for info on using java for example to do this or not? You need to scrap the information using regular expressions, or some other solution. Then you'll need to store the information in a database. The GUI is probably going to be the hardest part. I'll make a stock screening tool in my Android tutorial using SQLite very soon
I hope they help :) You're very welcome
Derek, next to your name, there is nothing better in this world than these videos.
That's funny :) Thank you for the compliment
Thank you for taking the time to tell me that :) It means a great deal to me that I was able to help!
Thank you :) You guys asked for them so I'm delivering
I was looking for an example in observer patterns and i got to say yours is really good! thanks mate.
This is crazy. So awesome. I had never learnt design patterns and used to fail in tech interviews. After the videos I can implement the factory and observer patterns so easily. Thanks a lot Derek.
I'm happy I was able to help :) You're very welcome.
I like the pace in these videos. I feel like a lot of other Comp Sci tutorials go very slow and I have trouble keeping interest. But never with Derek's videos!
+istainblack Thank you :) I do my best to be different and not waste peoples time.
I salute you for that sir!
You don't consider 22 minutes slow?
Thank you so much for this series of videos. You have saved me from failing at university, and I learned more in 20 minutes of your video than 2 hours in a lecture at uni.
Thank you for taking the time to tell me I helped :) I greatly appreciate it
I don't know why, but this guy just explains everything perfectly.
thanks Derek!!!
Thank you for the compliment :)
I'm usually don't comment on video , special on programming video , but your tutorial is the best i have seen. thank you
Thank you very much :) I appreciate you taking the time to tell me you liked it.
Thank you for taking the time to tell me I helped so much. I appreciate that :) I wish you the best on your exam and with your RUclips channel!
GREAT VIDEOS. Almost a month trying to understand from the book, but it wasn't clear in my mind until now. THANK YOU. It will indeed help me for my final coming up in two days
Thank you :) Yes in this situation you could do away with it. It is there so you can create numerous classes that would implement the Subject interface though. Maybe I should have taken it a bit further?
Thank you :) I have those files some place, but I'll have to dig them up. Do you just want the one for the observer pattern for now?
It is he view part of Model View Controller. Some people call MVC the Observer pattern, but that isn't by the book correct. Yes I have a separate tutorial on just the MVC pattern
Thank you very much :) I have a link to all the code in the description for the video. Feel free to do anything you'd like with it. I don't require you to even mention that it is my code.
I know this isn't new, but great job. I think they could be a bit shorter (depends the level at which the viewer is I guess), however for me 1.25 speed with occasional breaks to stare at code is perfect.
I rarely feel I have outstanding questions about the patterns after watching (watched a few), I guess all I have left is practice them... but the concepts are super well explained. Great job.
The best tutorial series. I can definitely say that the Software architecture class I learned in college( Master Level) was useless!!! This by far the best explanations on software design patterns. Thank you sir!
+Ontime Alarms Thank you for the nice compliments :) I'm glad I could help.
Funny how thanks to your set of tutorials I managed to go over all of the programming which will be covered during the 4 years of my education in just few months...Wish my university professors would explain things like you and provide similar examples...
continuing from my previous comment....Rather than adding specific stocks, I'd like to be able to observer any number of stocks and possibly get stocks based on different properties such as user entered conditions (daily range, sector, etc...). Just throwing out some ideas and would like to hear your general thinking...thank you again.
Not yet, but everything done in java can be done in c++ and c#. A c and c++ tutorial is in the works
Hey Derek,
Ancient video I know but afaik you don't need a "private Subject stockGrabber" in the StockObserver.
You can simply do the following:
public StockObserver(Subject stockGrabber){
...
stockGrabber.register(this);
}
this way you don't need to save a reference of the Subject in each observer. unless you have another reason to hold a reference of the Subject in the Observer AND a reference of the Observers in the List in the Subject ?
But anyway, nice video
You have the best programming tutorials I have seen on RUclips! You have a way of speaking that makes everything extremely easy to understand. Keep up the good work :)
Thank you very much :) I try to do my best.
You're very welcome :) I'm glad it helped
Derek, thanks so much for all these super helpful videos! One quick question, how do you auto complete so many codes this fast? Where should we learn the shortcuts?
Hey Derek. I'm a software engineer my self and just wanna say I really enjoy you videos. They are really well made and you really know your stuff :)
Please keep it going, because this is great stuff. I know you will help a lot of coders out there. So a million thumbs up for the great job :)
You're very welcome :) Best of luck on the exam
I have to make an MVC tutorial. I'll do that by next week. Thank you for the request :) It is already pretty much in the design patterns tutorial, but I should have cleared it up and made a proper tutorial
Derek, I appreciate so much the effort you put in your videos! I love the pace of them and that you mention the bigger context that was so often lost on the power point slides of university. I have started my first job and will have my (hopefully) last exam next week. Didn't even bother with the slides, when I saw that you have a series on design patterns.
Thank you for the great compliments :) Congratulations on your last exam!
Hey Derek! Question, in GetTheStock you're using == to compare strings, isn't it better practice to use .equals()? And why did you opt for == instead? (:
Cheers!
I agree too, "IBM".equals(stock); is the better than `==`
Hey again thanks for the video. It is advantageous to declare first a List like this List observers; and then initialize it with observers = new ArrayList(); That's how I learned it..
That would be a great topic to cover once I get into game development. Thank you for bringing it up :) I also plan on making an extended algorithm tutorial that comes out every once in awhile. I can add this topic there as well
To be honest this is my fav channel right now. Btw could you make a video or answer here on how to write fast in Eclipse? Plugins, shortcuts etc etc.. Thank you very much for your videos.
Thank you :) Im glad you liked it
HI Derek! Your videos are awesome! In this video you mentioned some more videos on stocks, mentioning videos 19 and 32. Now I went through those videos and in the java tutorial and many others as well as some of your refactoring videos, but can't find anymore videos using stocks as examples as you did in the Observer design pattern. Can you share what playlist I can find java tutorial videos using stocks as examples? I am a decent stock trader and am working on a program to automate some of my trades while improving on my Java.
Design pattern was a Rocket science to me till now. After your videos its just a cake walk for me :)
Thank you :) Im glad you found it useful.
Awesome tutorial. I liked the way you used "publisher" and "subscriber" as opposed to "subject" and "observer". It makes so much more sense with publisher and subscriber. I guess it's because subject and observer are more abstract names.
Best tutorial series ever. I lost some classes (at college) cause I had to travel. So I started searching and ended on Derek's channel. Just got a college work finished by watching the Android for Beginners Tutorial. I just wanna say: Thank you Bro, for taking your time and making this AWESOME, extreme good quality material to us and for free! Regards, Rafael - from Brazil.
Thank you for the nice compliment :) I'm very happy that you found the videos useful. You're very welcome
Thanks a lot, Dude!
You could definitely check if the observer is valid.
Thank you so so much. Now, I am confident, I will master DP after watching all of your DP tutorials. Do you have video tutorials on Java and Jee6?
i have been lost in the design patterns but you guided me, thanks man thanks alot.
I'm happy I could help :)
Very nice tutorial, thanks.
Just as side note, I would suggest to use ruby to describe design patterns, just to come out with less code, and more focused on pattern :)
Thank you :) I hope to cover more on DPs soon. Maybe I'll use Ruby
God bless you as well :) Thank you for the kind words. They mean a lot
So simple!
What an awesome pattern. I know it's used all the time, I have worked within Systems built on this principal but first time designing my own. Great video!!
I love software development pattern / best practices tutorials
Thank you :) I'm happy you enjoyed it
7:57
Thanks for another awesome video. A question: what have you enjoyed more, being in IT or finance? I'm thinking about these two career paths and will probably combine them but I wonder what you think as you were/are in both. Thanks!
Excellent video. I love the way you explain the pattern with threads. you make the explanation very clear and simple (which is a hard job considering that threads are a concept not very easy for beginners).
Thank you very much :) I did my best
Thank you very much :) I do my best
I think you are looking for the Factory pattern. You use the Factory design pattern when you want to define the class of an object at runtime.
Great video again Derek! I love your tutorials and appreciate that you teach more advanced topics, not just the basics. I was curious about the social network tutorial you mention at the end of this video, you said there is a link to it but I can't find one. Am I just being dumb?
Sorry Derek for my bad english, but I wonder if you know the design pattern with the repository! Do you have something similar on the serie? Because I learn so much when I watch and listen your tutorial and I also want to understand this pattern! Thank you!
Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "197,01"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.valueOf(Double.java:504)
at Observer2.GetTheStock.run(GetTheStock.java:63)
at java.lang.Thread.run(Thread.java:724)
And likewise for Thread 1 and 2.
Thank you :) I'm very happy to have been able to help.