2

In my database I have a table that looks like this Master(ID, Name, Attr1...). Every ID will have at least 1 entry in additional tables that have additional attributes. Those tables specialize the master table.

Spec1(MasterID, Attr2,...)

Spec2(MasterID, Attr3,...)

...

So, not sure how to map this to object oriented design. If every ID could have just one specialization then it would be easy, master would be abstract class and specializations would be concrete inherited classes but in this scenario, single ID can have all specializations and they could be added or removed during his lifetime in the database. Each specialization has unique business rules ofc.

1 Answer 1

3

In most cases it not very useful to make modeling decisions with names like "Master" or "Spec1" (one has to see the actual names and have a notion about their intended semantics). But in your case it seems pretty obvious that you are looking for an aggregate relationship between the class/table "Master" and the classes/tables "Spec1", "Spec2". It may be either a "1 to {0,1}" or a "1 to {0,n}" relationship, which depends on how many Spec records/objects you want to allow per Master record/object.

Dont' try to use "inheritance" for such cases. Inheritance is for static forms of specialization, not for specializations which can change at run time.

3
  • If I understand correctly, all specializations would share a common interface for communicating with a master and master would keep a list of all current specializations with their attributes.
    – Nezreli
    Jan 22, 2014 at 13:47
  • @Nezreli: honestly, if a common interface makes sense (or not) can only be deciced by the knowing the real names / semantics of "Master" and "Spec" - you probably missed that part in the first sentence of my answer. Technically, its also possible to have individual aggregate relationships between "Master" and Spec1, Master and Spec2 and so on.
    – Doc Brown
    Jan 22, 2014 at 14:20
  • Common interface is the only way, I think, if the whole system should be extensible without any changes to the master class. Real name of master class would be article I suppose. Specialized tables would add additional data to the article and it would define it's role in the system. Roles would be something like spare part, retail merchandise, food, official clothing...
    – Nezreli
    Jan 23, 2014 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.