本文介绍了为什么属性改变监听器而不是可观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在课堂设计中遇到问题,直到我发现可观察(使用观察者设计模式),从而创建了一个解决我的问题的小应用程序。我很高兴和自豪,我使用了一个很好的原则来解决一个问题。

I was having problems with class design until i found out about observable (using observer design pattern) and thus created a small application using it which solved my problem. I was happy and proud that I had used a good principle to sovle a problem.

现在我即将开始我的主要应用程序,刚刚阅读这个

Now i am about to start my main application and have just read this

制作JFrame和可观察对象

为什么海报建议不要使用observable,而是被告知要使用propertychangelistenr?使用observable有什么问题吗?

Why is the poster advised against the use of observable and instead told to use propertychangelistenr? Is there any issues with using observable?

Regards

推荐答案

观察者和听众模式非常相似。但观察员有一个缺点:所有的观察都是一样的。您必须实现基于 instanceof 的逻辑,并将具体类型的对象转换为 Observable.update()方法。

Observer and Listener pattern are very similar. But Observer has a weakness: all observables are the same. You have to implement the logic that is based on instanceof and cast object to concrete type into Observable.update() method.

听众是不同的。有很多监听器类型。例如鼠标监听器,键盘监听器等。每个人都有几个回调方法(即 keyPressed() keyReleased()等等)。所以,你永远不需要实现应该将问题是我的事件回答到事件处理程序中的逻辑。

Listeners are different. There are a lot of listener types. For example mouse listener, keyboard listener etc. Each one has several callback methods (i.e. keyPressed(), keyReleased() etc). So, you never have to implement the logic that should answer the question "is it my event" into the event handler.

我认为这就是为什么收听者模式更好。

I think that this is why listener model is preferable.

这篇关于为什么属性改变监听器而不是可观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:31