Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is n:1 observable:observer with generics possible in java? ( observer pattern )

I would like to listen your opinions about applying observer pattern.

My goal is to implement one concrete observer class that can listen multiple observable.

If I develop each different observer like below, then it would be simple like below.

public class ConcreteObserver implements EventObserverA , EventObserverB {

    updateEventA(EventA event) {}
    updateEvetnB(EventB event) {}

}

In this case, I should write many different observer/observable classes/interfaces with almost same code snippet inside.

To avoid hassle like above, I'd like to generify observer/ but as you guys know multiple inheritance rule does not allow code like below.

public class ConcreteObserver implements Observer<EventA> , Observer<EventB> {

    update(EventA event) {}
    update(EventB event) {}

}

Because I have at least double digit Observable events to observe in one concrete observer, I want to avoid implementing every observer/observable pair separately if possible.

I might guess there could be a better pattern for this case than observer pattern, since observer pattern is designed for n:1 observer:observable while my case needs 1:n observer:observable .

Do you guys have any idea/suggestion for this case?

like image 266
BinaryProbe Avatar asked Apr 29 '15 23:04

BinaryProbe


People also ask

Why are observers no longer used in Java?

Ans: The Observable class and the Observer interface have been deprecated in Java 9 because the event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications.

Can an Observer be an observable?

An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable 's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.

Can you have multiple subjects in observer pattern?

Note that subject and observers have a one-to-many relationship — multiple observers can all subscribe to one subject. And while it's the observer's responsibility to subscribe and unsubscribe to the subject, the subject is responsible for keeping a list of subscribed observers, and notifying them of changes.

How does the observer pattern work in Java?

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.


1 Answers

With the magic of Java 8, your ConcreteObserver doesn't necessarily have to actually implement Observer<EventA> and Observer<EventB>; if you write something like this:

public class ConcreteObserver {
    observeEventA(EventA event) {}
    observeEventB(EventB event) {}
}

then you can use ::updateEventA or ::updateEventB on a ConcreteObserver instance to get a method that is automatically convertible to the Observer<EventA> or Observer<EventB> functional interface. For example, you can write any of the following:

  • Observer<EventA> eventAObserver = concreteObserver::updateEventA;
  • Observer<EventB> eventAObserver = new ConcreteObserver()::updateEventB;
  • observerRegistry.register(EventA.class, concreteObserver::updateEventA);
like image 157
ruakh Avatar answered Sep 30 '22 16:09

ruakh