MVC:嵌套模型的属性更改事件
我正在构建一个 GUI 应用程序,并尝试尽可能遵守 MVC 原则。
因此,我的模型在 PropertyChangeSupport
的帮助下触发 PropertyChangeEvent
,以便 GUI 知道何时更新内容。
我的应用程序的模型是嵌套的,即我有一个主模型类,其中包含其他模型类的一些属性或列表,而这些模型类又可能包含更多模型类。
一个简单的例子:
public class MainModel {
private int someData;
private List<Stuff> stuffList;
// imagine PropertyChangeSupport and appropriate getters/setters
// for both MainModel and Stuff
}
现在 MainModel
和 Stuff
都有 PropertyChangeSupport
。如果有人侦听从 MainModel
触发的事件,它会获取 someData
的更改以及向列表 stuffList
添加/删除的操作。
但是,如果有人想要从对 stuffList
的各个元素所做的更改中获取事件,该怎么办?有两种可能性:
- 观察者必须获取
Stuff
元素列表并分别注册为每个元素的侦听器。 - 当添加
stuffList
元素时,主模型将自身注册为元素的侦听器,并将这些事件转发到主模型的侦听器。
这就是第一种方法的样子:
mainModelInstance.addListener("new stuff element", new PropertyChangeListener() {
public void propertyChanged(PropertyChangeEvent evt) {
Stuff s = (Stuff) evt.getNewValue();
s.addListener( // ... and so on
);
}
});
我认为 1. 具有保持模型干净和愚蠢的优点,但会导致代码重复(许多 UI 元素必须监听 stuffList
的更改并添加动态地添加到新的 Stuff
元素,见上文)。 2. 则相反:客户端代码并不那么混乱,但模型部分充当侦听器,这感觉不对。这就是为什么我目前使用第一种方法。
你有什么想法?也许我对自己太苛刻了,2.没关系。或者也许有一种完全不同(而且更好)的方法?
I'm building a GUI application and try to adhere to the MVC principle as good as I can.
Therefore my model fires PropertyChangeEvent
s with the help of PropertyChangeSupport
so the GUI knows what to update when.
The model of my application is nested, i.e. I have a main model class that contains some properties or List
s of other model classes which in turn might contain more model classes.
A simple example:
public class MainModel {
private int someData;
private List<Stuff> stuffList;
// imagine PropertyChangeSupport and appropriate getters/setters
// for both MainModel and Stuff
}
Now both MainModel
and Stuff
have PropertyChangeSupport
. If someone listens to events fired from MainModel
, it gets changes of someData
and from adding/deleting to/from the list stuffList
.
But what if someone wants to get events from changes made to the individual elements of stuffList
? There are two possibilities:
- The observer has to fetch the list of
Stuff
elements and register as a listener to each element separately. - The main model registers itself as a listener to the elements of
stuffList
when they are added and forwards these events to the listener of the main model.
This is how it looks like with the first approach:
mainModelInstance.addListener("new stuff element", new PropertyChangeListener() {
public void propertyChanged(PropertyChangeEvent evt) {
Stuff s = (Stuff) evt.getNewValue();
s.addListener( // ... and so on
);
}
});
I think 1. has the advantage of keeping the model clean and dumb but leads to code duplication (many UI elements have to listen to changes to stuffList
and add themselves dynamically to the new Stuff
elements, see above). With 2. its the opposite: The client code is not as messy but the model acts partly as a listener which somehow doesn't feel right. That's why I currently use the first approach.
What are your thoughts? Maybe I'm too harsh on myself and 2. is okay. Or maybe there is a completely different (and better) way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少 20 年以来,我一直在编写 MVC 模型并观察其他模型(例如现在称为 MVP[resenter] 或 MVVM 模式的做法),以下是我提供的一些启发式方法...
(1) 视图和控制器是分层的,GUI 事件处理很早就认识到这一点,方法是让事件侦听器在分层结构的不同级别进行侦听(例如直接在按钮级别或整个网页级别)。每个事件都指定与该事件关联的最具体的组件,即使正在侦听(也称为观察)更高级别的容器。据说事件会“冒泡”。
(2) 模型同样可以是分层的。模型生成的“更新”事件也可以使用与上述相同的技术,在事件中指定与更新事件关联的最具体的“内部模型”,但允许在“外部”复合模型级别进行观察。观察者更新事件可以“冒泡”。
(3) 分层模型有一个通用范例……电子表格。每个单元格都是一个模型,观察其公式中引用的其他模型/单元格。
For at least 20 years, I've programmed MVC models observing other models (like for doing what is now called MVP[resenter] or MVVM patterns), and here are some heuristics I'd offer...
(1) Views and Controllers are hierarchical, and GUI event handling has long recognized that by letting event listeners listen at different levels of the hierarchy (like directly at a button level, or at the entire web page level). Each event specifies the most specific component associated with the event even if a higher level container was being listened to (aka observed). Events are said to "bubble up".
(2) Models can equally be hierarchical. The Model-generated "update" event can also use the same technique as above, specifying in the event the most specific "inner model" associated with the update event, but allowing observing at the "outer" composite model level. Observer update events can "bubble up".
(3) There is a common paradigm for hierarchical models...the spreadsheet. Each cell is a model that observes the other models/cells referenced in its formula.