Use of Inheritance and composition

Use of Inheritance and composition

Old forum URL: forums.lhotka.net/forums/t/2412.aspx


joel posted on Thursday, February 22, 2007

I'm a bit confused about the advise that inheritance be used to model objects that share behavior, not data.  I have trouble making the distinction.  Here is my situation.  I am writing a web interface to an existing medical record system, so the database design is pretty much already determined.  This is a medical records application, which typically has a different database table for each type of medical record (ie physical_exam, chemistry_test, radiology_exam, etc).  Since there are many tables that may contain data for a given patient, we have a medical_record table that catalogs all of a patient's medical records.  All of the various exam/treatment tables have a foreign key pointing to the medical_record table, which include the dates the record was created and updated, the users that modified the record, record type, description, and a foreign key pointing to the patient/visit record.  So far, this is all shared data.  Does that mean I should not use inheritance to model the business objects?  For example, a physical_exam is-a medical_record, but does it behave-as a medical_record?  The purpose of my physical_exam business object would be to allow display and data entry of physical exam data.  I tend to want to have my physical_exam class inherit from medical_record so I don't have to reimplement properties for each field of data stored in the medical_record table and so I don't have to reimplement code to update the medical_record table.  Should I use composition/delegation instead?

From what I've read on this forum so far, I expect to be advised to not use inheritance based on what I've said so far.  I have two additional requirements that may qualify as "behavior".  All medical records should have the ability to be "finalized", which means that they are electronically signed and can no longer be modified (a bit field in the medical_record table).  Is that data or behavior?  Also, I want to implement a record locking system whereby each record can only be edited by one user at a time.  I intend to add a is_locked bit and lock_workstation_name fields to the medical_record table to prevent users from overwriting each others data. 

What is your advise?  Composition or Inheritance?  Can you point me to csla examples?

Thanks,

Joel

 

 

ajj3085 replied on Friday, February 23, 2007

Hey Joel,

I think you're on the right track.  The only behavior you described so far is the Finalize of the medical record and possibly the locking behavior.

When determining behavior, its sometimes helpful to think about what methods the objects will have (independent of what data they will need to carry out those methods).  

Your question about whether or no a physical exam behaves as a medical record will depend on the use case.  Will your uses be using the physical exam to perform the same operations that a medical record would?

Keep in mind, you may even end up with many PhysicalExam classes, if there are different uses cases that interact with a physical exam but do so in widely varying ways.

Properties are cheap and can even be generated and / or copy and pasted.  But perhaps MedicalRecord doesn't require a certain field to have any value, but physicalExam might.  That's an example of differing behavior, but in both cases the property declartion is the same (and the easiest part of the class to build).

HTH

Copyright (c) Marimer LLC