如何面对包含可观察字段的可观察对象

发布于 2024-09-04 06:59:43 字数 561 浏览 5 评论 0原文

我需要有关 MVC 和观察者模式的提示。

例如,模型包含类“Address”和“Person”。 Address 类包含字段 street:String、zipcode:String、location:String。而 Person 类包含字段 name:String、firstName:String、address:Address。

到目前为止我的方法看起来像这样: 地址和人员都是可观察的。如果调用它们的 setter 之一,我会验证当前值和新值是否不同。仅在这种情况下才会触发更新事件。该事件包含源、更改字段的名称、旧值和新值。

视图的类包含用于显示和编辑人员信息的文本字段:姓名、名字、街道、邮政编码、位置。它知道 Person 模型并且是该 person 的订阅观察者。因此它从 person 对象获取更新事件。

我的问题涉及 person 类中类型 Address 的地址字段,因为地址本身是可观察的。 如果在设置新地址时视图从人员那里收到更新事件,我可以更新视图中所有与地址相关的字段。 但是如果地址的某个字段发生变化怎么办?视图是否还应该从该地址注册更新事件?

任何有关常见设计方法的提示将不胜感激。 问候。

I need a hint concerning MVC and Observer-Pattern.

For example a model contains the classes "Address" and "Person". The Address class contains the fields street:String, zipcode:String, location:String. Whereas the Person class contains the fields name:String, firstName:String, address:Address.

My approach so far looks something like this:
Both, Address and Person are observable. If one of their setters is being called, I validate whether the current value and new value differ. Only in this case an update event is fired. The event contains the source, the name of the changed field, the old and the new value.

The class for the view contains text fields to display and edit the information of a person: name, firstname, street, zipcode, location. It knows the Person model and is an subscribed observer for the person. So it gets the update events from the person object.

My questions concerns the address field from type Address in the person class, since an address is observable on its own.
If the view gets an update event from person when a new address has been set, I can update all of the address related fields in the view.
But what if a field of the address changes? Should the view also register for update events from the address?

Any hints about common design approaches would be appreciated.
Greetings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-09-11 06:59:43

IMO 视图可以根据需要注册为任意多个模型对象的观察者。一般来说,我不知道为什么观察者应该被限制为只能观察单个可观察的任何原因。

我看到的唯一问题是,由于观察者只能实现接口一次,因此不同的可观察对象将使用相同的通知方法,这将使处理更加尴尬。

此外,如果一个 Observable 是另一个 Observable 的成员,则更新观察者注册的逻辑会变得更加棘手。

IMO the view can register as observer to as many model objects as it wishes to. I don't know of any reasons in general, why an Observer should be limited to observe a single Observable only.

The only problem I see is that since an Observer can only implement the interface once, the same notification method would be used by different observables, which would make handling more awkward.

Moreover, if one Observable is a member of another, the logic to update Observer registrations becomes a bit more tricky.

树深时见影 2024-09-11 06:59:43

我经常使用 JFace 数据绑定,它们是这样实现的:

您在模型元素和 GUI 元素之间创建绑定。为了简单起见,我们在模型中使用一个字符串字段,创建一个到 GUI 上的文本对象 (Textfield) 的绑定。因此,每当模型中的值发生变化时,就会触发一个事件,可以验证和转换该值,并通知文本小部件。绑定在另一个方向上执行相同的操作:在 GUI 上的每次编辑时,都会触发、验证和转换事件(如果需要)并捕获可观察模型字段。

所以典型的情况是模型字段和编辑器组件之间存在 1:1 的关系。

I've used JFace databinding quite often and they do it this way:

You create a binding between a model element and a GUI element. To keep it simple, we take a String field in the model a create a binding to a Text object (Textfield) on a GUI. So whenever the value in the model changes, an event is fired, the value may be validated and converted and the Text widget is notified. The binding does the same in the other direction: on every edit on the GUI an event is fired, validated and converted if needed an catched be the observable model field.

So the typical case is that there is a 1:1 relation between a model field and an editor component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文