I'm looking at a very large object within the application I'm working on, which is there to basically hold all the data together at a large level. Recently, we have started moving some methods out of other objects on the ModelData
to the ModelData
itself, because these methods are changing drastically now that we are implementing the ExistingModelData
(refer to code below if you haven't already).
public RemoveStuffAction //Called from View
{
public void doWork(HttpServletRequest request, HttpServletResponse response)
{
SessionData sessionData = WebUtils.getSessionData(request);
//We actually use contracts here for validation and such
//Passes contract to a service - ultimately resulting in..
ModelData modelData = sessionData.getModelData();
modelData.removeStuff(request.getParameter("stuffID"));
//other cleanup
//the old Code
//ModelData modelData = sessionData.getModelData();
//modelData.getStuffManager().removeStuff(stuffID);
//other cleanup
//save of model..
//forward mapping..
//exception handling..
}
}
public abstract ModelData
{
//lots of stuff
//Methods to modify the stuff
public void removeStuff(String stuffID) //example
{
this.getStuffManager().removeTheStuffFromHere(stuffID);
}
}
public NewModelData extends ModelData
{
//not much stuff
}
public DuplicateModelData extends ModelData
{
//not much stuff
}
public ExistingModelData extends ModelData
{
//quite a bit of stuff
//Overrides many methods to modify the stuff
public void removeStuff(String stuffID) //example
{
//we need the data there because we need to know what used to be there,
//even though it is supposed to now be removed
//what determines whether we need to know what was there or not is determined by
//whether this is ExistingData or not.
this.getStuffManager().getStuff(stuffID).markAsRemoved();
}
}
Basically, instead of creating overriding classes for all the Objects that could be changed on the model and implementing new Remove
methods (and then worrying about all the times we also Add
those objects so we add the correct one in each of those spots) we are moving these methods to the ModelData
class and letting that one overridden class handle the removal of our Objects through inheritance.
What we have works fine, and its pretty clear to everyone on the team whats going on. I also feel with the JavaDoc we've got on ModelData
that things are explained pretty well, but it just starts to bother me that we are moving so many methods to these classes - making already large classes even larger in terms of amount of text.
I've considered creating two new objects just to hold all these methods, and place the original version on the ModelData
and an overridden version on the ExistingModelData
, but I don't do it because I feel like it doesn't actually accomplish anything - it just shortens the amount of code in that particular class, which only makes my OCD feel a little better - and then I'll have to add an extra method call everywhere we need to use the methods in the new objects.
I also recognize that I could be too close to the situation right now and might be missing something obvious.
Is there something I could do which makes more sense and creates a cleaner design?