Having trouble getting the observer pattern working

最后都变了- 提交于 2019-12-08 02:13:38

问题


I have been trying to no avail to get the observer pattern working in a relatively simple application.

I have 4 GUI classes

  • StarterClass (contains a CompositeWordLists and a CompositeWordListData)
  • CompositeWordLists (contains many CompositeListItem/s and a CompositeWordListData)
    • CompositeListItem
  • CompositeWordListData (Contains a DialogWordData)
    • DialogWordData

Here is my Observable

interface Observable<T> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    void notifyObservers();
}

And I am creating Observers like this:

public class Observers {
    private Observers(){};

    interface WordListsObserver {
        public void update(CompositeWordLists o);
    }   

    interface ListItemObserver {
        public void update(CompositeListItem o);
    }
}

Basically I am having trouble with specifying the sort of event that occurred. For example, the CompositeWordLists class needs to know when a CompositeListItem is deleted, saved edited etc but I only have one update method ... my brain hurts now!

What is a better way of doing this?


UPDATE

Still having trouble with this, I added events and changed Observable and Observers but now I have type safety problems.

public class Observers {
    private Observers(){};

    /**
     * @param <T> the object that is passed from the Observable
     */
    interface ObservableEvent<T> {
        T getEventObject();
    }


    /**
     * Get notified about Authentication Attempts
     */
    interface ObserverAuthenticationAttempt {
        /**
         * @param e true if authentication was successful
         */
        public void update(ObservableEvent<Boolean> e); 
    }


    /**
     * Get notified about a Word Deletion
     */
    interface ObserverWordDeleted {
        /**
         * @param e the id of the word that was deleted
         */
        public void update(ObservableEvent<Integer> e); 
    }
}

The Observable Interface now looks like this

interface Observable<T> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    <K> void  notifyObservers(Observers.ObservableEvent<K> e);
}

The problem is that when I implement this I get and would have to cast K to the appropriate type, not really what I want to do.

@Override
public <K> void notifyObservers(ObservableEvent<K> e) {
    for(Observers.ObserverAuthenticationAttempt o : this.observers)
        o.update(e);
}

What am I doing wrong?

update 2

Actually it works better with an Observable like this, but I still need to specify the correct EventType in two different places.

interface Observable<T,K> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    void  notifyObservers(Observers.ObservableEvent<K> e);
}

回答1:


You do not need to parametrise the Observers, but you need to parametrize the events.

public interface Observer<T> {
    void notify(T event);
}

An example event:

public class WordListUpateEvent {

    private final int changedIndex;

    public WordListUpateEvent(int changedIndex) {       
        this.changedIndex = changedIndex;
    }

    public int getChangedIndex() {
        return changedIndex;
    }
}

Then you can have different interface of it for example:

public interface WordListObserver extends Observer<WordListUpateEvent> {}

and its implementations

public class ConcreteWordListObserverA implements WordListObserver {
    @Override
    public void notify(WordListUpateEvent event) {
        System.out.println("update item at index: " + event.getChangedIndex());
    }
}

on the other hand you need your Observable interface, i have splitted it in two interface in order ti make the notifyObservers method not public to the observers (you will see it later):

public interface Observable<T> extends ObservableRegistration<T> {  
    void notifyObservers(T event);
}

public interface ObservableRegistration<T> {

    void addObserver(Observer<T> o);
    void removeObserver(Observer<T> o);
    void removeAllObservers();
}

If you would have several observables in a subject, you can not implemnt the Observalbe interface direct to your subject, so you need a seperate implementation class:

public class ObservableImpl<T> implements Observable<T>{

    private final List<Observer<T>> observers = new ArrayList<Observer<T>>();

    @Override
    public void addObserver(Observer<T> o) {
        this.observers.add(o);
    }

    @Override
    public void removeObserver(Observer<T> o) {
        this.observers.remove(o);
    }

    @Override
    public void removeAllObservers() {
        this.observers.clear();     
    }

    @Override
    public void notifyObservers(T event) {      
        for(Observer<T> observer : observers) {
            observer.notify(event);
        }
    }

}

Now you can use the implementation in your subject:

public class Subject {

    private Observable<WordListUpateEvent> wordListObservable = new ObservableImpl<WordListUpateEvent>(); 

    //private Subject<OtherEvent> otherObservable = new ObservableImpl<WordListUpateEvent>();


    public ObservableRegistration<WordListUpateEvent> getWordListObservableRegistration() {
        return this.wordListObservable;
    }

//  public ObservableRegistration<OtherEvent> getOtherRegistration() {
//      return this.otherObservable;
//  }

    public void doSomething() {
        this.wordListObservable.notifyObservers(new WordListUpateEvent(42));
    }

}

And this is how you can connect the observer and the subject:

public class Start {

    public static void main(String[] args) {
        Subject subject = new Subject();

        subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());
        subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());

        subject.doSomething();
    }
}



回答2:


I would create an Observer interface, containing a public void update(ObservableEvent oe) method, and an ObserverEvent interface. After that, you can create specific class for each of your events.




回答3:


http://download.oracle.com/javase/1.4.2/docs/api/java/util/Observer.html
http://www.java2s.com/Code/Java/Design-Pattern/Observableandobserver.htm

The Java Observer's update method has the Object argument. You can pass any Object, thus you can create your own "UpdateMessage" Object that can contain the updated object and additional information about what happend (deleted, saved etc.).



来源:https://stackoverflow.com/questions/4283304/having-trouble-getting-the-observer-pattern-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!