tags:

views:

105

answers:

7

I have object called Foo. Right now it implements IFoo which has a lot of properties on it.

I have one class that only depends on a few of those properties so i create IMeasurableFoo (which just has a few properties)

to avoid duplicate code, i now have IFoo : IMeasurableFoo as i moved the properties into IMeasurableFoo

but this feels wrong from an inheritance point of view as you have a more generic interface inheriting from a specific interface

any thoughts on the best way to organize these abstractions

For example, if these were concretions:

Bird would not inherit from FlyingBird (it would be the other way around)

+5  A: 

No, it's not "more generic interface inheriting from a specific interface". IMeasurableFoo, having only several properties, is the generic interface, whereas IFoo is more specific, since it refines the knowledge about the IMeasurableFoo. Think IList inheriting from IEnumerable.

Anton Gogolev
the reason i say this is that Bird would not inherit from FlyingBird? it would be the other way around
ooo
+1  A: 

Don't use interface inheritance for the sole purpose of avoiding duplicate code. If there is no relation between the two intefaces, they don't need to implement inheritance.

If Foo is not a MeasurableFoo there's no inheritance needs.

Fabian Vilers
+1  A: 

No, I don't think there's something wrong with it. Probably, it's just the name of IMeasurableFoo that's misleading since it's just measurable and it's not a complete Foo.

Mehrdad Afshari
+4  A: 

Maybe change the names? IMeasurableFoo sounds like a more specific interface indeed, but couldn't you just call it IFoo and rename the original IFoo interface? Or rename both?

Is the IMeasurableFoo really an IFoo or (perhaps) an IMeasurable?

Lennaert
Then IMeasurable would be akin to IDisposable, +1
Patrick McDonald
A: 

I don't think that IMeasurableFoo is less generic. Since IFoo includes IMeasurableFoo, I would argue that IMeasurebleFoo is more generic.

It really feels like you have IBaseInterface and IExtendedInterface : IBaseInterface.

In other words, I think you have the correct approach.

EDIT: With your example IBird and IFlyingBird, you simply have a naming problem. It should be:

public interface IFlyer
{
    void Fly();
}

public interface IBird : IFlyer
{
    void Chirp();
}
Brian Genisio
A: 

In cases like this I think the naming of the thing is the problem rather than the thing itself. In other words, an interface with less members tends to be more abstract than an interface with more members.

Andrew Hare
A: 

What is strange here? Interface inheritance increases the number of methods and properties for each level of inheritance.

Rename IMeasurableFoo to IBar and more specificity/more genericity sense will gone

abatishchev