mercredi 29 avril 2015

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(Observer<EventA> event) {}
    update(Observer<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?

Aucun commentaire:

Enregistrer un commentaire