سؤال

I had a long class which I couldn't split up because I need all the fields in one database table (Google AppEngine + Objectify). I'm already using embedded classes wherever possible. The class consists mostly of getters and setters plus the logic behind them.

In order to get functional chunks I decided to use repeated inheritance. Now it looks like this:

  • MyStoredModel extends
  • AbstractSettingsModel extends
  • AbstractHierarchyModel (dealing with parent/child objects) extends
  • AbstractInformationModel (holds title, description, ...) extends
  • .....
  • AbstractModel

It's easier to see what every class is doing and I'd also say it's easier to test. The downside is the "inheritance chain".

Is that considered bad behavior? What are be better ways to make the class smaller?

هل كانت مفيدة؟

المحلول

I'd think about using composition for these. So the model consists of Settings, Hierarchy, Information objects.

It does mean you have to include all the setter/getters on the top-level object, which would then delegate to the respective setters/getters in each of those component classes, but at least you can isolate all the handling of them into separate classes without the complexity of an inheritance hierarchy. And it also means those component classes are now free to inherit from anything they like.

نصائح أخرى

As long as the inheritance makes sense in a logic perspective, I'm pretty sure that's the ways it's supposed to be.

But composition can also be a good idea.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top