Java 接口可观察

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11404034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:57:23  来源:igfitidea点击:

Java Interface Observable

javainterfaceobservable

提问by user1216750

I have the following:

我有以下几点:

  1. A Java class: ClassA implements Observer

  2. A Java Interface: Inter (extends Observable, not possible)

  3. A Java class: ClassB implements Inter extends Observable

  4. A Java class: ClassC implements Inter extends Observable

  1. 一个 Java 类:ClassA 实现了 Observer

  2. Java 接口:Inter(扩展 Observable,不可能)

  3. 一个 Java 类:ClassB 实现了 Inter extends Observable

  4. 一个Java类:ClassC实现Inter extends Observable

Now the code for ClassA is somewhat like this.

现在ClassA的代码有点像这样。

ClassA{
    Inter anyClass = new ClassB();
           //or it could be
    Inter anyClass = new ClassC();
    //Inter.addObserver(this); //not possible since Interface cannot extend Observable
}

Now if a particular event happens in ClassB or ClassC, I want ClassA to know about it. I thought of using the Observer/Observable but the problem is that the Interface cannot extend Observable.

现在,如果某个特定事件发生在 ClassB 或 ClassC 中,我希望 ClassA 知道它。我想过使用 Observer/Observable 但问题是接口不能扩展 Observable。

If someone understands the question please help me find a way to update ClassA when something happens in ClassB or ClassC.

如果有人理解这个问题,请帮助我找到一种在 ClassB 或 ClassC 发生某些事情时更新 ClassA 的方法。

回答by Jap Mul

You don't specifically have to use the built-in Observer/Observable java implementation. You can create your own interfaces to use the observer pattern.

您不必特别使用内置的 Observer/Observable java 实现。您可以创建自己的接口来使用观察者模式。

Create an interface Observer with the method 'update' or 'notify' (or something like that) which will be used for ClassA

使用将用于 ClassA 的方法“update”或“notify”(或类似方法)创建一个接口 Observer

then to use the 'Inter' interface to act as an Observable make sure it implements the following:

然后使用“Inter”接口作为 Observable 确保它实现以下内容:

- registerObserver(Observer)
- unregisterObserver(Observer)
- notifyObservers() //(or updateObservers(), which calls the update/notify method for all registered observers)

Make sure classes that implement the 'Inter' interface also have an arraylist called observerCollection to keep track of the observers. Then everytime something changes in ClassB or ClassC and you want to tell ClassA (the observer) you can call notifyObservers() to let it know something has changed.

确保实现“Inter”接口的类也有一个名为observerCollection 的数组列表来跟踪观察者。然后,每当 ClassB 或 ClassC 发生变化并且您想告诉 ClassA(观察者)时,您可以调用 notifyObservers() 让它知道某些事情发生了变化。

Take a look at this to see what happens with the observer design pattern: http://en.wikipedia.org/wiki/File:Observer.svg

看看这个,看看观察者设计模式会发生什么:http: //en.wikipedia.org/wiki/File:Observer.svg

回答by Seraphin

Simply define your own interface IObservablewith the appropriates methods addObserverand deleteObserver:

只需使用适当的方法addObserverdeleteObserver定义您自己的接口IObservable

public interface IObservable {
    void addObserver(Observer o);    
    void deleteObserver(Observer o);    
}

Your interface can extend this IObservableinterface:

你的接口可以扩展这个IObservable接口:

public interface inter extends IObservable {
...
}

In your class A and B, the addObserverand deleteObservermethods will be inherited from the Observable class and do not need to be defined.

在类 A 和 B 中,addObserverdeleteObserver方法将从 Observable 类继承,不需要定义。

public class A extends Observable implements inter {
...
}

回答by Ale Zalazar

By the 3 and 4 conditions I assume that Observable is a class, because ClassB and ClassC are extending it. So, why don't you put the addObserver method in Observable class? Then you could assign ClassB or ClassC instances to an Observable anyClass variable, and register the observer via addObserverMethod:

根据 3 和 4 条件,我假设 Observable 是一个类,因为 ClassB 和 ClassC 正在扩展它。那么,为什么不将 addObserver 方法放在 Observable 类中呢?然后你可以将 ClassB 或 ClassC 实例分配给 Observable anyClass 变量,并通过 addObserverMethod 注册观察者:

ClassA implements Observer {
    Observable anyClassB = new ClassB();
       //or it could be
    Observable anyClassC = new ClassC();
    anyClassB.addObserver(this);
    anyClassC.addObserver(this);
}