A design ( inheriteance / overriding ) question

S

sourabh

Hi
Perhaps this question does not fit in this news group but I need some advice
and could not figure out where to post this.
This is a design / inheritance qus.
I have class Task.
This is a base class for some specific tasks

Say
WorkOrderTask : Task
TimeSheetTask : Task

Now Task has a function GetDetails() which gets all info about task. This
makes a database call.
I override this in the derived class. For implementing this in derived
class i see following 2 approaches.

Approach 1. WorkOrderTask .GetDetails()
{
Calls base.GetDetails(); to get base details, populates in the
object
Gets details specific to WorkOrderTask, populates in the object
returns;
}

Approach 2. WorkOrderTask .GetDetails()
{
Get details specific to WorkOrderTask as well as the base class
details, populates in the object
returns;
}

The problem with
Approach 1
I have to make 2 database calls.
Approach 2
If theres some change in base class I need to take care of these in all the
derived classes as well.
Whereas, Approach 1 is advantageous here as by modifying only the
Task.GetDetails funciton ( this is the base class ) function i have all the
values and i do not need to touch the derived classes.



Was thinking this might be a very common problem, so perhaps many of might
be able to guide me on the approach i must take here. Perhaps anything other
then the two i have talked about.

Thanks in advance
Sourabh
 
A

Alok Jain

Dear Sourabh,

I will be in favor of approach 1. As you have rightly mentioned approach 2
puts an high dependency on class 2 which is inherited from class1. Approach
2 assumes that it knows every attribute of class1, here if you change any
attribute of class1 you will have to update class2. And I feel this also
breaks the encapsulation for class1 (although due to inheritance
relationship it has a legal way to do that).

I agree that due to two database calls there will be performance impact in
approach 1. But things are changing now and whenever we encounter a
situation of Scalability versus Performance, we need toconsider that there
is a higher cost with Scalability while with bigger machines Performance is
not that big issue now.

There could be one more hybrid approach where you can generate a single
database call in Class2 but it delegates the responsibility of constructing
Class1 dependent part (query construction and data population) to Class1
itself. Never tried this but you can give it a try.

Please let me know if any body has any idea on this.

Thanks
Alok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top