Chapter 4. Watch What’s Going On with the Observer and Chain of Responsibility Patterns

<feature> <title>In This Chapter</title> <objective>

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

Get Design Patterns For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.