vendredi 24 juin 2016

Mark modified object and unmark it from outside

This is kind of a follow up of this question of my own.

I have a Tournament class which has a List<Event>, the categories of the event. You can instantiate a tournament and then make changes on its events:

Event event = new Event(...);
Tournament tournament = new Tournament(..., new ArrayList<>(Arrays.asList(event)));
event.setProperty(property);

However, the Tournament class is part of a resolution process that calculates all the possible schedules it has, one by one. The model the solver class uses to compute the schedule is based on the state of the tournament (and its events) in the moment the resolution process begins (calling tournament.solve()).

We can retrieve subsequent schedules (the next solution) by invoking tournament.nextSchedules(). And here's the problem. As I said the solver holds the configuration of the tournament from the instant that solve() was called. If now we start changing properties of any event (which is allowed), the current model of the tournament, the instance, will be no longer consistent with the model the solver holds. This has to be managed, otherwise I will be getting wrong schedules, or even errors.

Now to the point: as I was suggested in the previous question, I tried implementing a solution to track any modifications made in the events. For that I made the Event class extend Observable and setChanged() is being called from every method that modifies properties (or those relevant to the model in the solver anyway). Here's a sample:

public Event extends Observable implements Validable {
    public void setProperty(Property property) {
        //...
        setChanged();
    }
}

Having done that, now I need a way to reset the changes flag on the observable event when they're considered in a final form. And when does this happen? Well, whenever tournament.solve() is called. When trying to solve the problem and retrieve the schedules, the tournament (therefore, its events) are in a consistent state. The problem is that to clear the changed flag on a event, I need to use the clearChanged() method and it is not public, which kind of makes sense because it should be up to the observable object to determine if it's changed or not anymore. In my scenario though, it's actually up to a different class, the Tournament class, to say if an event has changed or not.

I could very well define a public method in the Event class wrapping the clearChanged() method, but this would break the whole point of the Observable interface as I understand it.

What changes should I make in order to allow a Tournament to notify its events that they have no longer changed?

Aucun commentaire:

Enregistrer un commentaire