I use pattern Observer, but found that it getting difficult to inherit from classes have listeners. If in inherited class also exist listeners very difficult controll which listeners work. Does exist pattern similar to Observer but with better control of listeners? Thanks.
-
1Sorry, I didn't understand your problem. Can you provide some sample code which demonstrates your Observer Pattern implementation? – MD Sayem Ahmed May 24 '12 at 08:54
-
it's the **observer pattern** and not the other way around. putting that aside, what do you mean with "very difficult controll which listeners work" ? – May 24 '12 at 08:55
2 Answers
Seems like you don't want to use inheritance to implement Observer Pattern.
If I am not mistaken on the above point, then you could always implement this using interfaces. Just declare two interfaces for the Subject and the Observer, and then have your classes implement those.
For example, your Subject could look something like this -
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
Observer -
public interface Observer {
public void update(int age, float salary); // imaginary parameters.....
}
Then, your concrete subjects can inherit from the above Subject
interface -
public class SubjectImpl implements Subject{
private ArrayList observers;
public void registerObserver(Observer o){
observers.add(o);
}
public void removeObserver(Observer o){
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObservers(){
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update(30, 200.50); // imaginary arguments, provide your own...
}
}
}
and your Observer
implementation -
public class ObserverImpl implements Observer{
public ObserverImpl(Subject s){
s.registerObserver(this);
}
public void update(int age, float salary){
// implementation
}
}
and then you can use -
Subject s = new SubjectImpl();
Observer o = new ObserverImpl(s);
// do whatever you want now
This approach is certainly better than Class Inheritance
because in this case both your subject and observer can inherit from other classes, providing a more flexible design.
If your question is about providing java listener implementation without inheriting, then you can use Annonymous Classes for this purpose -
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Anonymous class example");
}
};
yourButton.addActionListener(listener);

- 28,628
- 27
- 111
- 178
-
This is a known "problem" in Java: the observer pattern is implemented with the `Observable` abstract class, which causes you not to be able to extend any other classes. This is a good Java Interview question. The interface alternative is the way to go. – Brady May 24 '12 at 09:11
-
@Brady: Yup, you are right. However the OP said that he/she is implementing the pattern, so I assumed that class inheritance is being used here, so I thought interface inheritance could be a better alternative. – MD Sayem Ahmed May 24 '12 at 09:15
Signals and Slots is an improvement to the Observer design pattern.
Its used quite extensively in Qt (C++) as described here.
Here is a related question for Signals and Slots for Java and an implementation in Java.