English 中文(简体)
难以找到观察员模式开展工作
原标题:Having trouble getting the observer pattern working

我一直试图让观察员模式在相对简单的应用中发挥作用。

我有4个全球倡议课程

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

这里我可以观察

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

我正在设立这样的观察员:

public class Observers {
    private Observers(){};

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

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

基本上,我难以确定所发生的事件。 例如,<代码>CompositeWordLists 班级需要知道何时使用<代码> 《复合材料被删除、保存、编辑等,但我只有一种更新的方法......我现在的脑受伤!

如何做到这一点?


UPDATE

尽管如此,我还是增加了一些事件,改变了观察和观察员,但现在我有安全问题。

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); 
    }
}

可观测 座谈现在就这样看。

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

问题在于,当我执行时,我会而且必须将K归入适当的类别,而不是我想要做的事情。

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

我做了什么错误?

update 2

实际上,它能更好地发挥类似的作用,但我仍然需要在两个不同地点具体说明正确的事件。

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

你们不需要贬低观察员,但你需要使事件的不平衡。

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

实例:

public class WordListUpateEvent {

    private final int changedIndex;

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

    public int getChangedIndex() {
        return changedIndex;
    }
}

然后,你可以有不同的接口,例如:

public interface WordListObserver extends Observer<WordListUpateEvent> {}

目 录

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

另一方面,您需要 可观察的接口,即将其分成两个接口,以便提送通知 观察员方法不公开(见后文):

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();
}

如果你在某个主题上有多个观测站,你就不能将观测站与你的主题直接连接起来,因此你需要一个适当的执行类别:

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);
        }
    }

}

现在,你可以把执行作为主题:

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();
    }
}
问题回答

我将设立一个观察员接口,其中包含一个<编码>的公开空白更新(可操作的电子)方法和观察员。 活动接口。 之后,你可以为你们的每一项活动创造特殊类别。

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

Java观察员的最新方法有异议。 你可以通过任何目标,从而创造你们自己的“最新” 能够包含最新物体的目标和关于所发生事件的更多信息(删除、节省等)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签