Program structure and 'Command pattern'

Jun 26 2007 3:45 AM
Hi,
This is hard to explain, but it is a simple enough problem...

I have a large dataset (Class 'Dataset') that will be altered by a sequence of events (Abstract class Event, not events in the sense of delegate events). I need to be able to reconstruct the state of this dataset after any amount of events, so I designed an Event abstract class which will be extended for every type of event that can happen. An instance of the specific type is created, applied to the dataset, and stored in a list whenever an event occurs.

Now, it is quite important that the rest of my program does not modify any of the data without constructing an Event, otherwise if I recreate the dataset at that point in time (by re-applying all the events in my list) it will not be consistent with the original.

At the moment my Event has the abstract method Apply(Dataset data), which applies the changes for that event to the given dataset. However, this means I have to keep a most of Dataset's members public, and (besides being bad OO practice) if I or someone else accidentally modify any of them w/o an Event, it's going to be quite hard to debug.

My other option would be to include an overloaded method in Dataset that applies each Event accordingly, but I'm not sure how it will apply. For example, if I have extended Event to EventA, and do:

Event e = new EventA();

Dataset.ApplyEvent (e);

and Dataset has two ApplyEvent methods:

public void ApplyEvent (Event e) {}

public void ApplyEvent (EventA e) {}

Will the overloaded method for EventA be called? Or is there a simpler, elegant solution I'm not thinking of?

Sorry for the long post and thanks for your help :)

Answers (2)