Link between BO and GUI is broken. What to do?

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.

Answers


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

Need Your Help

Japanese Characters in Java App

java jbutton jlabel

I'm trying to put Japanese characters into jLabels and jButtons, but I've a problem because in my develop machine it works fine but if I bring the jar to other computer doesn't work.

VB.NET Edit and Continue how prevent dialog to popup every time had exception?

vb.net visual-studio-2010

On all version of VS (currently 2010), When Exception occur, the popup dialog appear and say:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.