在优雅的方式来处理循环事件在Java中? [英] The Elegant way to handle Cyclic Event in Java?

查看:171
本文介绍了在优雅的方式来处理循环事件在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这不是一个具体的问题给我;每个人都可能遇到过这个问题。
要正确地说明吧,这里有一个简单的用户界面:

i think this not a specific problem to me; everybody might have encountered this issue before. To properly illustrate it, here's a simple UI:

正如你可以看到,这两个纱厂正在控制单个变量 - A。唯一的区别是,他们用它不同的看法控制。

As you can see, those two spinners are controlling a single variable -- "A". The only difference is that they control it using different views.

由于这两个微调显示值是同步的,循环事件出现了。

Since these two spinners' displaying values are synchronized, cyclic event shows up.

如果我改变微调上方,A将被改变,底部微调的价值也将随之更新。 然而,更新底部微调的调用(如setValue方法)也将触发另一个事件指示顶部微调的基础上底部微调的数值进行更新。因此,创建了一个恶性循环,可最终导致出现StackOverflow例外。

If i change the top spinner, "A" will be changed and the bottom spinner's value will also be updated accordingly. However, updating the bottom spinner's call (such as setValue) will also trigger another event instructing the top spinner to update based on the bottom spinner's value. Thus creates a bad cycle which can eventually cause a StackOverFlow exception.

我的previously解决方案是有点烦琐:我把一个守卫布尔值,表明第二个更新呼叫是否应该执行

现在我想问问的我怎么能很好地处理这种情况呢?一般,没有具体到纺织

THX

因为我已经有2答案提示我要利用观测的结构,我必须说什么。

Since i've got 2 answers suggesting me to utilize the observer structure, i have to say something about it.

就像我说的,这是伟大的,但远远不够完善。这不仅是因为其固有的复杂性,还它无力解决的问题

Like what i've said, it's great but far from being perfect. Not only because of its inherent complexity, but also Its inability to solve the problem.

为什么呢?要了解原因,你必须实现的Java Swing的视图和模型的控制器的的紧耦合的。让我们我微调UI的例子。假设变量A实际上是一个观察的对象。然后,从顶部微调发射的第一个状态改变事件之后,观察家A将更新其值,并触发一个PropertyChange事件通知底微调。然后是第二个更新的更新底部微调的观点。的然而的,不断变化的底微调的观点不可避免地触发一个多余的事件,将再次尝试将A的价值。随后,致命的循环完全构造和堆栈溢出将被抛出。

Why? To see the reason, you must realize the tight coupling of the View and Model-Controller in Java Swing. Lets take my spinner UI for an example. Suppose the variable A is actually an Observer object. Then, after firing the first state change event from the top spinner, the Observer "A" will update its value and fire a PropertyChange event to notify the bottom spinner. Then comes the 2nd updating which updates the bottom spinner's View. However, changing bottom spinner's view inevitably triggers a redundant event that will try to set "A"'s value again. Afterwards, the deadly loop is fully constructed and the stack overflow will be thrown.

在理论上,该观察模型试图通过引入2个独立的反馈路径来解决直接循环。链接的更新赔率(事件响应codeS)隐含形成大桥连接两个路径,再制作周期。

In theory, the Observer model tries to solve the direct cycle by introducing 2 independent feedback paths. The chained updating odds(in event-response codes) implicitly form a bridge connecting both paths, making a cycle again.

推荐答案

让我们回到模型 - 视图 - 控制器,想想你的模型是什么,你的观是什么。

Going back to Model-View-Controller, think about what your Model is, and what your View is.

在当前的实现中,您有两种模式(每个微调控件),他们是通过视图层正在同步。

In your current implementation, you have two models (one for each Spinner control), and they're being synced through the View layer.

什么,你应该做的,虽然是共享了同一个后备模式。对于有跌价微调,创建一个代理原始模型。即:

What you should be doing though is share the same backing model. For the spinner with a subtracted value, create a proxy to the original model. ie:

class ProxySpinnerModel implements SpinnerModel {
    getValue() { return originalSpinner.getValue() - 10 }
    setValue(v) { originalSpinner.setValue(v+10) }
}

spinnerA = new JSpinner()
spinnerB = new JSpinner( new ProxySpinnerModel( spinnerA.getModel() ) )

现在,你并不需要添加的听众,因为他们俩都工作过同型号的默认实现(originalModel)已经改变监听器,它触发到视图。

Now, you don't need to add listeners, since they're both working off the same model and the default implementation (the originalModel) already has change listeners which it fires to the view.

这篇关于在优雅的方式来处理循环事件在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆