如何在Java可观察(MVC)中观察同一类的多个数据? [英] How to observe multiple data of same class in Java observable (MVC)?
问题描述
这是我想到的2种选择.
Here's 2 option that i thought of.
-
具有多个模型,每个模型代表一个类.如果我有2 int 对象,我需要创建2个可观察的对象并进行视图检查 可观察的改变了.
Have multiple models each representing one class. If i have 2 int object, i need to create 2 observable and have view checking which observable is changed.
在单个可观察对象(模型)中,将相同对象的多个对象分组 类到数组.鉴于我将不得不迭代数组并 更改包含数组中所有数据的所有字段.
In a single observable (model), group multiple object of the same class into Array. In view i will have to iterate the array and change all fields containing all data in the array.
我开始学习如何使用Java Observable和Observer实现MVC模式.我发现我提到的2种方法不是最佳解决方案.希望可以对此有所帮助.
I am starting to learn how to implement MVC pattern, using java observable and observer. I find the 2 methods which i have mentioned is not the best solution. Hope can get some help on this.
修改: 例如,我在observable类中有以下方法,有人可以教我如何在观察器中编写update()以获得int b的值吗?
Example, i have the following methods in observable class, can someone show me how to write the update() in observer to get the value of int b?
public void setIntA(int a){
this.intA = a;
setChanged();
notifyObservers(a);
}
public void setIntB(int b){
this.intB = b;
setChanged();
notifyObservers(b);
}
setIntA(5);
setIntB(3);
推荐答案
在Observer类上为a和be创建两个引用,您可以使用这样的update命令.
Create two references on the Observer class to a and be and you can use update command like this.
public void update(Observable o, Object arg)
{
if (o == a)
{
//do what you want with arg
}
}
这篇关于如何在Java可观察(MVC)中观察同一类的多个数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!