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.
All of the code follows the video to help you learn.
If you like videos like this, please tell Google [googleplusone]
Sharing is nice [nttfblike]
Code & Comments from the Video
SUBJECT.JAVA
OBSERVER.JAVA
STOCKGRABBER.JAVA
STOCKOBSERVER.JAVA
GRABSTOCKS.JAVA
GETTHESTOCK.JAVA
I can see my comments here only if I provide my name and email. Now this comment is only to enable me see my previous comment and responds if any. I use (Firefox & Google Chrome).
I have to approve all comments before they show up. I do this for security reasons. Sorry about any confusion. I promise you that I read every comment and reply to 95% of them
Hi Derek,
Am I getting this right? In the threaded version of the code, all that the runnable object does is to change the stock prices eh? As soon as a thread changes the stock price, the original notifyObserver() is called on the stockGrabber which then sends an update wave invoking the registered observer’s update right?
P.S. Derek, I absolutely love your vidz, I am following the GoF and HFDP along with your vidz and they work fantastically well. Things that I don get in GoF – you make em so clear thru the tutes. God bless you for such wonderful work.
Thanks,
Pralay
Hi Pralay, Yes you are getting the idea. I don’t use just one book like most people seem to? I actually combined 8 sources for these tutorials. I’m glad you found them useful. Just understand that in 2 parts of this tutorial I actually deviate from the patterns used in the GOF book, for a different form that I found more useful from other sources. The Bridge and Interpreter patterns are the patterns I’m alluding to. Feel free to ask questions and I’ll do my best to help
Can There Be Multiple Subjects??
Yes you could have many subjects
Also,
I know I am asking way too much here. But do you think it would be possible to make some vids on big-data and stuff. I mean, say the first few lectures are more of ppts of Hadoop etc. And then slowly you get into mapreduce stuff. And show some examples, say the word count problem or terasort may be! It is so much in demand these dayz – any company that I interview with throws in a couple of questions on hadoop etc.
Thanks yet again,
Pralay
Interviewers are asking about Hadoop? Wow, I just started looking into it in the last few months because it has only been stable since last month! I’ll see what I can do. I’m planning on covering algoritms very soon
Please, provide PPT, didn’t fully understand for a Chinese like me, poor english,
Best videos on this topic by far!!!
Fast, Clear, and to the point!
I am translating them to vb.net and Spanish and have been successful so far (observer and strategy) to use in my vb.net class as exercises for the students.
Before your videos, I was not able to clearly understand any of the design patterns.
Thanks a lot!!!
Note: I only removed the threading part to keep it focus and simple. π
I’m very happy that you are enjoying them π I worked to make everything understandable by pulling ideas from ever source and then simplifying. If you give me a bit more information on your other question I’ll do my best to help
What is the best way of changing the code so that the observers are only monitoring one of the three stock (each its own)?
Thanks!
I’m going to be covering how to use the design patterns in my refactoring tutorial which will start very soon. I’d give you an answer now, but I’m not clear on what you are trying to do. I hope I provide your answer in that tutorial
Best explanation of design pattern ever !!
Thank you π
Very good explanation.Thanks.
You’re very welcome π
Ah Derek ! The More I go through your tutorials the more a voice echoes in my head “This guy’s a genius π ” Amazing π
Thank you very much for the compliment, but I’m not all that smart. I’m sure you are probably smarter then me π
This tutorial is very helpful! But I am having problems understanding it as I do not know java. Do you have a copy of this code for C++ instead of java? Or do you have a link to another observer tutorial for c++? I have been looking around for one but none of them are as good as your tutorial.
Sorry, but I don’t have a C++ version of my design patterns tutorial. I will cover C++ at some point and cover it again.
Hello,
May I ask why in the uml diagram IBMPrice and AAPPrice look like class that extend Observer.
Hi,
As you mentioned there is loose coupling between observer and subject but from code it seems that they are tied with each other..can you please clarify. Apology in advance for my absurd question.
Thanks
Kunal
That isn’t an absurd question. When trying to avoid coupling there are always trade offs. The goal is to eliminate dependence as much as possible, but you still need to provide a way for objects to interact. With experience you’ll learn to find the sweet spot that you are most comfortable with.
Thank you very much for such a nice series of lectures. I have almost gone through all of your lectures on design patterns. They are wonderful and knowledge increasing.
One thing that I can not understand is that in the uml the IBM and AAPLprice are shown as classes whereas in the code I don’t see anything like a class by these names.
Will you please make it clear what is it in reality.
Appologies in advance for this question if it does make a sense.
You’re very welcome π Those stocks basically are just the data being passed around. They could actually represent pretty much any type of data in this pattern. I hope that helps. Sorry I couldn’t get to you quicker
Hi!
Im quite new at programming and theres something i cant understand. Why arent “events” used in the observer pattern(java) like it is in c#. Basically im trying to figure out the main differences between observer pattern in java vs c#.
There are many different forms for these design patterns. They are guides rather then exact recipes
Hi Derek, I have observer pattern working which updates the GUI. I am in a situation where my model completes an operation and it wants to ask user if they want to save the results? It notifies GUI to bring up this dialog but I think GUI sending feedback from this notification would be against the norms and will cause problems (it will send all observers to display the dialog for a start which is bad!). Should the ‘SaveResults’ thing be launched from GUI after operation is complete? This operation (Say Operation1)can be performed from several dialogs and I would like it to have the same behavior of asking user if they want to save results. Can I package that as re-usable code somehow? Thanks.
Hey there,
I’m getting the following error:
” Exception in thread “Thread-1” Exception in thread “Thread-0” Exception in thread “Thread-2” java.lang.NumberFormatException: For input string: “677,59” “.
I’m guessing this has to do with me living in Sweden where we use commas to separate the decimals instead of dots?
If that is indeed the case, how could I make it work?
Thanks!
Yes that is the reason for the error. You could use a regular expression, or there is more information on localization with the DecimalFormat here. i hope that helps π
Hello Sir,
I took exact code but till 18th line of GrabStock.java to check simple (without threading) functionality.
But I am receiving following error,
New Observer: 1
Exception in thread “main” java.lang.NullPointerException
at controller.StockGrabber.register(StockGrabber.java:14)
at controller.StockObserver.(StockObserver.java:29)
at controller.GrabStocks.main(GrabStocks.java:14)
Please suggest!
Thanks.
Sir, got the error fixed. StockGrabber constructor was missing from my code to instantiate ArrayList.
Thanks.
Great I’m glad you fixed it π
i followed along your tutorial but ,typed the codes along with you, but I cannot pass the stockGrabber as an argument when constructing StockObserver object(ie observer1)
StockGrabber stockGrabber = new StockGrabber(); StockObserver observer1 = new StockObserver(stockGrabber);//ERR
because: StockObserver in class StockObserver cannot be applied to given types:
Required: Subject
Found: StockGrabber
Try copying all the code that is under the video. That should solve that problem.
Derek, you’re the man! You really saved my life with your tutorials! π
Keep it up. You are a brilliant teacher.
Best regards from Germany.
Hello Germany π Thank you very much. I’m very happy that I could help.
U’re Amazing bro.. All so simple when u explain it ^^thanks much^^ Happy to watch your series! ο»Ώ
Thank you very much π I’m very happy that I can help.
Good Explanation ! Worth Watching. Thanks a ton. U can include examples of Design Patterns used in JDK since its based on Java in each pattern.
Eg: All implementations of java.util.EventListener (practically all over Swing thus)
Thank you π
Hi Derek!! Great videos and explanation!!! Thanks very much π
I have been trying to apply patterns on a simple project that I am working on. I have this make make payment page on my app and I need the different fields like Card holder name, card number, expiry date and cvv to listen to keypad which is the subject. I am not sure if observer pattern is the best I can use here.
I need only the name field to be updated wen I enter a name on the keypad object but I am making all by observers get notified about it through this observer pattern. Is it the right way to implement. Please help me and correct me if I am wrong.
Thank you.
Thank you very much π The goal is always to use patterns only when they make code more readable and flexible. If you ever feel as if you are complicating your code through the use of a pattern then it probably shouldn’t be used. I have an object oriented design tutorial that may help?
Love this
Thank you π
U are really amazing… Ur explained is too good and simple. All these days I was thinking design patterns as Mount Everest. U made it simple. Thanks a lot.
Thank you π I’m very happy that I could help
dude ur awesome…:)
thanks alot..:)
at first when i tried reading from google i never understood a thing..:(
thanks alot for videos.Concepts explained are crystal clear..:)
If possible can you provide some programs to work on simple programs, it would be really helpful..:)
Thanks again..:)
Thank you π I do my best. I’m planning to make a ton of free Android apps that will allow everyone to practice everything I have on my site. I’m working on them
Hi Derek,
Thanks a lot for amazing set of tutorials.
Thank you π You are very welcome
You’ve really helped me pass my exams with these tutorials!
Thanks a lot!
You’re very welcome π I’m glad I could help.