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.

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

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

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

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

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.

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

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#.

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.

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)

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)

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 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?

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..:)