Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Java's Observable class? [closed]

Tags:

I'm coming to Java from the C# world where the Observer pattern is implemented as a first-class language construct with the event keyword.

I see that Java's had the Observable class since the early days, but it clearly has implementation issues and doesn't seem to be widely used. Thus far I've just been rolling-my-own implementation of the Observer pattern in my Java code but I always can't help but think there must be a better alternative to always turning out this boilerplate code. There are the Listener classes in Swing but they don't seem appropriate for non-Swing code.

What's the recommended solution for this oh-so-common problem? Third-party libraries are ok with me.

like image 320
HolySamosa Avatar asked Apr 18 '12 22:04

HolySamosa


People also ask

What can I use instead of Observable in Java?

There are many alternatives of Observer design pattern and Reactive Streams is one of them. Reactive Streams or Flow API: Flow is a class introduced in Java 9 and has 4 interrelated interfaces : Processor , Publisher , Subscriber and Subscription . Flow.

What is Java's built in Observer pattern?

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. The object which is being watched is called the subject. The objects which are watching the state changes are called observers or listeners.

Which interface do we need to implement the use of Observer in Java?

Implementation With Observer The java. util. Observer interface defines the update() method, so there's no need to define it ourselves, as we did in the previous section. Here, the second argument comes from Observable, as we'll see below.

What is the difference between Observer and Observable?

Observer : Any object that wishes to be notified when the state of another object changes. Observable : Any object whose state may be of interest, and in whom another object may register an interest.


2 Answers

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.

like image 105
Joni Avatar answered Oct 14 '22 11:10

Joni


I recommend looking into reactive programming. Java has only one implementation that I know of: reactive4java.

like image 37
thSoft Avatar answered Oct 14 '22 11:10

thSoft