From the course: Java EE: Contexts and Dependency Injection

Unlock the full course today

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

Fire an event and observe it

Fire an event and observe it

From the course: Java EE: Contexts and Dependency Injection

Start my 1-month free trial

Fire an event and observe it

- [Instructor] Let's implement a stock trading system scenario that fires stock price changes throughout the system. And let's add a few observers that react to those price changes. So, the first thing that I need to do is create an event class. And this event class is going to be called PriceChangeEvent. Now this class is going to to carry some information about the change event. And because it's a stock, it should contain information such as the stock name, the current price, and the change in price. So, I'm just going to add these as private members here. So this is going to be my stock name. I'm also going to add, as well, the price. I'm just going to make it a float, just to keep the code nice and simple. And also, let's also have a price change. So this is going to be a float again, just to keep it nice and simple. I'm going to call this priceChange, like so. So I'm just going to generate some getters and setters. Like so. So I'll have setters and getters for all of these. And…

Contents