I'm designing a data visualizer class: given a data object, it will create a visual representation of it on the user's screen.
I have a few design restrictions I must adhere to:
- The class is to have a parameterless "Visualize" method. All the visualization settings are to be instance fields.
- The state of the class should always be accurate.
To illustrate:
public class DataVisualizer {
public DataObject Object { get { return _object; } set { // logic } }
public void Visualize(){
// visualize the data...
}
}
So far so good but the class is to also have a "DataObjectCollection" instance field. It's not necessary for the aforementioned field to be set in order to visualize the data object, but if it is, it will enable additional methods such as "VisualizeNext" and "VisualizePrevious" (which must modify the state of the visualizer and call Visualize).
To illustrate:
public class DataVisualizer {
public DataObjectCollection { get { return _dataObjectCollection; set { // logic } }
public DataObject Object { get { return _object; } set { // logic } }
public void Visualize(){
// visualize the data...
}
public void VisualizeNext(){
Object = NextObjectInDataObjectCollection();
Visualize();
}
public void VisualizePrevious(){
Object = PreviousObjectInDataObjectCollection();
Visualize();
}
}
Now remember design restriction #2 above. To give an example, if the user sets DataObjectCollection, during setting, we need to check what DataObject is. If DataObject isn't contained in DataObjectCollection, we need to set DataObject to either null, or the first element in DataObjectCollection. On the flip side, if we set DataObject, we need to check if DataObjectCollection contains DataObject. If not, we need to set DataObjectCollection to null.
To make things more complicated, I also need to track the index of the data object being visualized for the VisualizeNext / Previous methods. -1 for when the collection is null. (I can't use IndexOf because the collection could contain duplicate elements or null values.)
And to make matters worse, the DataObjectCollection is actually part of another collection! So in essence, our visualizer looks something like this:
public class DataVisualizer {
public DataObjectOuterCollection { get { return _dataObjectOuterCollection; set { // logic } }
public DataObjectInnerCollection { get { return _dataObjectInnerCollection; set { // logic } }
public DataObject Object { get { return _object; } set { // logic } }
public void Visualize(){
// visualize the data...
}
public void VisualizeNext(){
Object = NextObjectInDataObjectCollection();
Visualize();
}
public void VisualizePrevious(){
Object = PreviousObjectInDataObjectCollection();
Visualize();
}
}
Remembering point #2 above, things get complicated because everytime a field is set, I need to check the value of the other fields and set them to the correct values. For example, if InnerCollection isn't contained within OuterCollection, then I need to set InnerConnection to the first element of OuterCollection or null and then do the same thing with DataObject. I also need to keep track of the index of InnerCollection, too!
Is there any design pattern or code structure I can follow to make it easier to handle so much logic? When a field is only "linked" to 1 other field it's easy, but when it's 5 of them, it quickly becomes a lot to manage!
Aucun commentaire:
Enregistrer un commentaire