Data and Object Modeling 2: Sub/Super Types and Classes

2007 May 15
by Paul Marcotte
To go along with my previous woolgathering on the correlation between a database schema and a class package, here is another observation regarding class inheritance and sub/supertype tables. In object modeling, we describe the relationship between a subclass and superclass as "is a". The same goes for tables in a data model, where the subtype "is a" supertype. The obvious benefit of using inheritance in object modeling is code reuse. When we add, remove or alter a method of the superclass, the change is automatically inherited by the subclass. In a data model the benefit of using subtypes is arguable. The benefit are that your model better represents the domain, reduces data duplication and (depending on the extent of your normalization) eliminates NULL values. The trade-off is that you are thereafter required to use joins when reading records and transactions when creating, updating or deleting records.During a recent update to the reporting features for my employer, I decided to refactor several database tables into subtypes. I then organized my generated components (courtesy of the invaluable Illudium PU-36 Code Generator), into a package with a single (albeit smart) DAO and several subclasses. Using my Calendar example (for purely illustrative purposes), I ended up with a database tables like this: calendar.AthleticCalendar
calendar.Calendar
calendar.CalendarStatus
calendar.CalendarType
calendar.FacultyCalendar
calendar.StudentCalendar
And a class package like this: calendar.AthleticCalendar
calendar.Calendar
calendar.CalendarDAO
calendar.CalendarFactory
calendar.FacultyCalendar
calendar.StudentCalendar
One takeaway from this exercise is that the resemblance between the two now lends credence to the idea of using a schema to organize related tables. :-) Granted, experienced software developers will likely point out that the object model should in fact drive the data model, but I have far more experience with database design than I do with OOP, so I still approach design from the data model first. Has anyone out there had a similar experience with super and subtypes in data/object modeling? How do you first approach domain modeling, with a class diagram, or an E/R diagram?