Debate on use of sub interfaces and poly-morphism

  • Thread starter Christian Bongiorno
  • Start date
C

Christian Bongiorno

I am currently creating a set of interfaces for a rather large project
we have designed.
Many of the interfaces have the same method names and functionality.

such as:
public void addEventListener(TaskEvent evt); // Extends EventObject

public TaskDetails getDetails(); // task details extends IDetails

These imply a more specific class type but are repetative.
Then it occurred to me, I could create a super interface that contains
generic forms of these methods and have each sub interface extend
them.

example:

public void addEventListener(EventObject evt);
public IDetails getDetails();



The first method requires that implementors use this exact static
type. Such things as

public void addEventListener(TaskEvent evt);

would not satisfy the

public void addEventListener(EventObject evt);

And as for getDetails, although the user could assume a specific
subtype to caste to by the type of implementing class (getDetails() on
a Task object would return a TaskDetails obj -- not gauranteed, but
assumable) and callers would still have to type caste the return type
to get most functionality

So, basically the debate boils down to this:
Do I use generic types in a super interface to reduce copy & paste
(something I despise) or do I apply C&P to a few interface methods and
thus enforce specific subtypes and relieve the caller of casting? I
only intend for there to be maybe 10 interfaces to use these common
methods.

I am leaning toward the second choice.

Thoughts?

Christian
 
C

Chris Uppal

Christian said:
So, basically the debate boils down to this:
Do I use generic types in a super interface to reduce copy & paste
(something I despise) or do I apply C&P to a few interface methods and
thus enforce specific subtypes and relieve the caller of casting?

Doesn't it depend on /why/ the duplication exists ?

If the interfaces have these elements in common because they mean the same
thing, and because it is semantically /necessary/ for them to share those
members (so that, for instance, if one changes then they all change), then you
should express that fact by factoring out the common interface.

If, OTOH, it is only a sort of coincidence that they are so similar (as sounds
likely to me), then using common interface would be actively misleading.
(Especially in the absence of C++-style private inheritance, which can be used
for such cases where it is expedient to inherit functionality, but the
inheritance doesn't really /mean/ anything.)

-- chris
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top