BO 和 GUI 之间的链接已断开。该怎么办?

发布于 2024-08-23 21:46:54 字数 1031 浏览 12 评论 0原文

我有一些点(汽车停靠站)要在时间图上表示。 这些点之间通过线连接。点+线代表一个图(即汽车时刻表)。可以通过用鼠标及时移动停车站来修改图表。

我决定将点和线实现为控件(认为在面板上移动它们会更容易)。

我有两个业务对象层 - 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 CarCarStop 不关心也不知道 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 CarStops.

  • How do I move controls? Simply:
    Detect a mouse move on the panel and compute dX,
    transform dX in dTime
    Car.Move(dTime) – moves all the CarStops.
    When CarStop moved, send event to CarStopControl, and the latter change its coordinates. In this way CarStopControl 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 CarStopControls.

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 技术交流群。

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

发布评论

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

评论(1

苏别ゝ 2024-08-30 21:46:54

我的第一个想法是引入额外的间接级别。您的控件可以是代理对象的订阅者(实现相同的接口),而不是真正的 CarStopCar 对象,然后代理对象又知道它们的“真正”伙伴并被当他们的好友被新好友替换时,能够更新对好友的引用。

interface ICarStop {
    void addObserver(CarStopObserver observer);
    void removeObserver(CarStopObserver observer);
}

class CarStopControl implements CarStopObserver {
    public void update (ICarStop obj, Object arg) {
        // ...
    }
}

class CarStopProxy implements ICarStop {
    ICarStop original;
    public CarStopProxy(ICarStop original) {
        this.original = original;
    }
    public void setOriginal(ICarStop original) {
        this.original = original;
    }
    public void addObserver(CarStopObserver observer) {
        // ...
    }
    public void removeObserver(CarStopObserver observer) {
        // ...
    }
    public void notifyObservers(Object object) {
        // iterate through observers an update each
    }
}

class CarStop implements ICarStop {
    CarStopProxy proxy;
    public CarStop(CarStopProxy proxy) {
        this.proxy = proxy;
    }
    public CarStop(CarStop other) {
        // copy all properties
        this.proxy = other.proxy;
    }
    public CarStopProxy getProxy() {
        return proxy;
    }
    public void setProxy(CarStopProxy proxy) {
        this.proxy = proxy;
    }
    public void handleChange() {
        proxy.notifyObservers(...);
    }
}

现在,当您需要替换 CarStop 对象时,您可以这样写:

CarStop newCarStop = new CarStop(oldCarStop);
// update all references to point to the new object
oldCarStop.setProxy(null);

另一种可能性是引入 Mediator,它将能够通过某种标识符来识别模型对象(不同于它们的具体地址/引用,因为它可以改变)。在这种情况下,当 CarStop 对象被另一个对象替换时,新对象只会接管其前任对象的 ID,并在其更新消息中使用它:

class CarStop implements ICarStop {
    Mediator mediator;
    Long id;
    public CarStop(Mediator mediator) {
        this.mediator = mediator;
    }
    public CarStop(CarStop other) {
        // copy all properties
        this.mediator = other.mediator;
    }
    public void handleChange() {
        mediator.notifyObservers(id, ...);
    }
}

CarStop newCarStop = new CarStop(oldCarStop);
// update all references to point to the new object

My first idea is to introduce an extra level of indirection. Instead of the real CarStop and Car 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.

interface ICarStop {
    void addObserver(CarStopObserver observer);
    void removeObserver(CarStopObserver observer);
}

class CarStopControl implements CarStopObserver {
    public void update (ICarStop obj, Object arg) {
        // ...
    }
}

class CarStopProxy implements ICarStop {
    ICarStop original;
    public CarStopProxy(ICarStop original) {
        this.original = original;
    }
    public void setOriginal(ICarStop original) {
        this.original = original;
    }
    public void addObserver(CarStopObserver observer) {
        // ...
    }
    public void removeObserver(CarStopObserver observer) {
        // ...
    }
    public void notifyObservers(Object object) {
        // iterate through observers an update each
    }
}

class CarStop implements ICarStop {
    CarStopProxy proxy;
    public CarStop(CarStopProxy proxy) {
        this.proxy = proxy;
    }
    public CarStop(CarStop other) {
        // copy all properties
        this.proxy = other.proxy;
    }
    public CarStopProxy getProxy() {
        return proxy;
    }
    public void setProxy(CarStopProxy proxy) {
        this.proxy = proxy;
    }
    public void handleChange() {
        proxy.notifyObservers(...);
    }
}

Now, when you need to replace a CarStop object, you write:

CarStop newCarStop = new CarStop(oldCarStop);
// update all references to point to the new object
oldCarStop.setProxy(null);

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:

class CarStop implements ICarStop {
    Mediator mediator;
    Long id;
    public CarStop(Mediator mediator) {
        this.mediator = mediator;
    }
    public CarStop(CarStop other) {
        // copy all properties
        this.mediator = other.mediator;
    }
    public void handleChange() {
        mediator.notifyObservers(id, ...);
    }
}

CarStop newCarStop = new CarStop(oldCarStop);
// update all references to point to the new object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文