Skip to content
Ameen Abozaid
In reviewJNR-005-L07

Observer

  • Lesson 7 of 10
  • 11 min read
  • Junior
On This Lesson 4 topics

The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.

In this lesson

notify interested parts when something happens.

Planned topics

  • Problem: when an order is placed, email, stock, and analytics all need to know.
  • The subject keeps a list of listeners and calls each one.
  • The subject does not know what listeners do.
  • Do not rely on the order of listeners; that is a hidden trap.
  • Grows into event-driven design later.
Draft outline details

Demonstration

java
interface OrderListener { void onPlaced(Order o); }
listeners.forEach(l -> l.onPlaced(order));

Check your understanding

Should the subject know what listeners do?

Show answer

No; that is the point of the pattern.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.