From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

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

Challenge: The Observer pattern

Challenge: The Observer pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Challenge: The Observer pattern

(upbeat music) - [Instructor] Let's have a look at our first exercise. In this one, you're going to be refactoring an app that posts traffic updates for various cities. So let's have a look at the code so far. First of all, there is a class called City, which has a string field called trafficUpdate. And a method called updateTraffic to update that field. This class is going to be the observable. Then there's a class called TrafficUpdates, which is going to be the observer. This has an array list of strings called updates and a method called getUpdates, which prints out every update in the list. Finally, there is a Main class with a main method. In here, I'm creating two City objects, sunnyville and springfield. And I'm creating a trafficUpdates class and then I'm updating the traffic for both cities. So in Sunnyville, there is congestion in the town center and in Springfield, there's an accident on the highway.…

Contents