From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: The Observer pattern

Solution: The Observer pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Solution: The Observer pattern

(upbeat music) - [Instructor] Let's have a look at my solution to this exercise. If yours looks a bit different to mine, that's fine. I'm not expecting they'll be exactly the same. So let's have a look at the traffic updates class first, which is the observer. This now implements the property change listener interface, and it contains a method called property change. I'm passing in a property change event and inside this method, I'm adding that event to the list of updates. In here, I've now got a property change supports field. In the post traffic update method, I'm calling the file property change method on the support, and I've got a method called ads property change listener, where I can pass in a listener and add that listener to the support object. Finally, in the main class, in the main method, I'm adding the traffic updates objects as a listener to both the cities I've created. So now if I run this app again, in…

Contents