Chapter 4. Watch What’s Going On with the Observer and Chain of Responsibility Patterns
Using the Observer pattern
</objective> <objective>Creating observers and subjects
</objective> <objective>Using the Java Observable class
</objective> <objective>Using the Java Observer interface
</objective> <objective>Using the Chain of Responsibility pattern
</objective> </feature>The big boss comes into your office and says, “Someone’s been editing the data in our central database — why wasn’t I informed?”
“How’s that?” you ask. “You want to be informed of every edit that happens to the data?” You know the boss is new on the job, but this is something even more clueless than you’d expected.
“That’s right,” the boss says. “I want personal notification each time a record is changed in the database. I want to keep an eye on what goes on around here.”
“You mean, like a memo?”
“Right.”
“Hmm,” you say. “I think I have a better idea. How about I use the Observer design pattern and register you as a database observer?”
“Huh?” the boss asks.
“You’ll be notified each time the database is modified,” you say. “No memos needed. It’ll all be done automatically, in code.”
“That’s all I ask,” the boss says, leaving.
You smile to yourself as you turn to the code, wondering how happy the boss is going to be with about 200,000 notifications a day. But, by using the Observer design pattern, the coding won’t be hard to set up.
This chapter is about keeping your objects in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access