Java MVC 模式与 Observer/Observable
您好,
我正在构建的应用程序遇到问题。 给出我所处的场景。 我有这两个控制器都继承了从主控制器初始化的相同模型。 所有控制器都有自己的视图,但我只有一种模型。
问题在于。当该模型发生变化时。 如何通知另一个控制器(来自两个控制器)发生了更新? 我要使用 Observer/Observable 还是 PropertyChangeEvent?我对如何在 MVC 架构上实现这两者感到有点困惑。
非常感谢您就此事做出回应。
谢谢, 西里尔·H.
Greetings,
I have a problem in my application I'm building in.
To give the scenario I'm in.
I have these two controllers both inherit the same model initialize from a main controller.
All controllers have their on views but I have only one model.
To problem is that. When ever changes happens to that model.
How can I notify the other controller (from the two controllers) that an update occurred?
I'm a going to use Observer/Observable or PropertyChangeEvent? And how, I'm a little bit of confused on implement both on the MVC archictecture.
Your response is highly appreciated on this matter.
Thanks,
Cyril H.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个类似的情况,我使用 PropertyChangeSupport 来监听模型的更改。我认为最好的方法是创建一个包含私有 PropertyChangeSupport 和两个公共方法 addPropertyListener、removePropertyListener 和一个受保护方法 firePropertyChange。这些方法将用作 PropertyChangeSupport 方法的包装器。因此,您的控制器应该只添加PropertyListeners 以便监听公共模型的更改。
注:
I had a similar case where I used PropertyChangeSupport in order to listen to model's changes. I believe that the best way is to create an AbstractEntity that contains a private PropertyChangeSupport and two public methods addPropertyListener, removePropertyListener and a protected method firePropertyChange. Those methods will be used as wrappers of the PropertyChangeSupport's ones. So your controllers should just addPropertyListeners in order to listen to changes of the common model.
Note:
您的控制器应该只听模型的声音。 (PropertyChange 或其他)。为什么您希望您的控制器通知自己?
如果它是您想要通知的主控制器,它也应该只监听模型。初始化模型的不是它自己吗?
Your controlers should just listen to the model. (PropertyChange or something else). Why would you like your controlers to notify themselves ?
If it is the main controller you want to notify it should just listen the model too. Isn't it itself who initialize the model ?
我没有看到任何问题,
或者引入一个监听器,如果前者听起来不好。
I don't see any problem,
observable
, andOr introduce a
listener
, if former doesn't sound good to you.