BO 和 GUI 之间的链接已断开。该怎么办?
我有一些点(汽车停靠站)要在时间图上表示。 这些点之间通过线连接。点+线代表一个图(即汽车时刻表)。可以通过用鼠标及时移动停车站来修改图表。
我决定将点和线实现为控件(认为在面板上移动它们会更容易)。
我有两个业务对象层 - Real BO (CarStop
) 和 GUI Control (CarStopControl
)。 然后我将 CarStop
(Time, Station) 关联到 CarStopControl
(X, Y) - CarStopControl 订阅CarStop.Moved 事件。
最后,Car
对象有一些 CarStop
。
- 如何移动控件?简单地: 检测鼠标在面板上的移动并计算 dX, 在
dTime
中变换dX
Car.Move(dTime)
– 移动所有停车站。 当CarStop
移动时,向CarStopControl
发送事件,后者改变其坐标。通过这种方式,CarStopControl
似乎可以跟随鼠标的移动。
这就是全部。
问题出现在 Car.Move
中需要重新创建 CarStop
集合 - CarStopControl
之间的链接和 CarStop
显然已经过时了,car BO Car
和 CarStop
不关心也不知道 CarStopControl
。
类似的情况是当Car
本身可以被新车
替换时。
有人有类似情况吗?有没有+-的“解决方法”快速解决这个问题?
谢谢。
I have some points (car stops) to represent on a time graph.
These points are linked between them by lines. Points + Lines represent a Graph (that is a car schedule). The Graph can be modified by moving CarStops in time with the mouse.
I decided to implement Points and Lines as controls (thought that will be easier to move them on the panels).
I have two business object layers – Real BO (CarStop
) and GUI Control (CarStopControl
).
I associate then a CarStop
(Time, Station) to a CarStopControl
(X, Y) - CarStopControl subscribes to CarStop.Moved events.
Finally, a Car
object has some CarStop
s.
- How do I move controls? Simply:
Detect a mouse move on the panel and computedX
,
transformdX
indTime
Car.Move(dTime)
– moves all the CarStops.
WhenCarStop
moved, send event toCarStopControl
, and the latter change its coordinates. In this wayCarStopControl
seems to follow the mouse movements.
This is all.
The problem appeared when in Car.Move
there was a need to recreate the CarStop
collection – the links between CarStopControl
and CarStop
obviously became obsolete, car BO Car
and CarStop
does not care nor even know about CarStopControl
s.
The similar situation is when Car
itself could be replaced by a new Car
.
Had someone similar situations? Is there a "workaround" of +- quickly fix this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的第一个想法是引入额外的间接级别。您的控件可以是代理对象的订阅者(实现相同的接口),而不是真正的
CarStop
和Car
对象,然后代理对象又知道它们的“真正”伙伴并被当他们的好友被新好友替换时,能够更新对好友的引用。现在,当您需要替换 CarStop 对象时,您可以这样写:
另一种可能性是引入 Mediator,它将能够通过某种标识符来识别模型对象(不同于它们的具体地址/引用,因为它可以改变)。在这种情况下,当
CarStop
对象被另一个对象替换时,新对象只会接管其前任对象的 ID,并在其更新消息中使用它:My first idea is to introduce an extra level of indirection. Instead of the real
CarStop
andCar
objects, your controls could be subscribers to proxy objects (implementing the same interface), which then in turn know their "real" buddy and are able to update the reference to their buddy when latter is replaced with a new one.Now, when you need to replace a CarStop object, you write:
Another possibility would be to introduce a Mediator, which would be able to identify model objects by some sort of identifier (different from their concrete address / reference, as that can change). In this case, when a
CarStop
object is replaced by another one, the new one just takes over the ID of its predecessor, and uses it in its update messages: