Java Notes: Observer Pattern
Call me when something happens. The standard way to use a graphical user interface (GUI) is to define a method that is called when certain events occur -- for example when a GUI button is clicked.
Buzzwords. There are lots of terms that are used to describe or implement this simple idea.
- Event-driven programming - This is the basic idea that you turn control over to some framework, eg the Swing GUI framework in Java, and it will call you when something interesting happens. This is an example of inversion of control.
- Observer Pattern - Also called the Publish-Subscribe or Listener Pattern. This is explained below.
- Callbacks - This is the term that is typically used for C/C++ programs where the address of a function to call is passed to the framework. Java doesn't have function addresses, so uses a different solution using interfaces.
- Delegates - C++ has created the idea of "delegates" in the language to more closely approximate function addresses. It's a little more convenient than the standard Java solution with interfaces, but is used to accomplish the same thing.