观察者模式更新参数 [英] Observer pattern update parameters
问题描述
为什么观察者界面将 Observable o 作为参数? 您是否建议使用Javas现有类(实现Observer;扩展Observable)?
Why does the Observer interface has Observable o as a parameter? Do you recommend using Javas existing classes (implements Observer; extends Observable)?
public class Test implements Observer {
void update(Observable o, Object arg);
}
推荐答案
它接收Observable
引用,以便Observer
可以使用它来解决它将处理已通过的Object
arg的方式.同样,一旦完成工作,Observer
可以调用deleteObserver
删除自身.
It receives the Observable
reference so that the Observer
can use it to address the way it will handle the Object
arg that was passed. Also, the Observer
could call deleteObserver
to remove itself once it finished the job.
您不应该使用它们.告诉您的不是我,而是Java本身背后的人.检查一下:
You shouldn't use them. And it's not me that's telling you, it's the people behind java themselves. Check it out:
https://dzone.com/articles /javas-observer-and-observable-in-jd已弃用 https://docs.oracle.com/javase/9/docs/api/java/util/Observable.html
在Java 9中,已弃用Observer
和Observable
.
In Java 9, Observer
and Observable
are deprecated.
已弃用.这个类和Observer接口已经被 不推荐使用. Observer和Observable支持的事件模型是 相当有限,Observable传递的通知顺序为 未指定,并且状态更改不是一一对应的 带有通知.要获得更丰富的事件模型,请考虑使用 java.beans包.为了在线程之间进行可靠且有序的消息传递, 考虑在 java.util.concurrent包.对于反应流样式的编程, 请参阅Flow API.
Deprecated. This class and the Observer interface have been deprecated. The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package. For reactive streams style programming, see the Flow API.
查看其他答案: 观察者如何订阅可观察对象?
这篇关于观察者模式更新参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!