Usually the observer pattern is implemented with an ad-hoc solution when ever it's needed. When it comes to behavioral patterns it's one of the simplest: two methods to control a list of "observers," and one method that notifies the observers when something interesting happens.
The observer pattern tends to arise too infrequently outside of certain domains, and when it does arise it tends be too specific to the application domain, so generic solutions are of little value. You can see the problem with the java.util.Observable
class: if you inherit from Observable
you have to accept every possible kind of "observer" that may be passed to you, which likely makes no sense for your application:
- What if your object can produce two different kinds of events, and thus needs two different lists of observers?
- What if the observers may influence your object depending on the event, e.g. by being able to veto a change?
That said, EventBus from Google Guava seems to do a pretty good job of simplifying event producing and handling by taking a radically different approach.
Other techniques based on annotations have been discussed on SO before.