leora
leora

Reputation: 196791

interface question

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)

Upvotes: 0

Views: 238

Answers (7)

abatishchev
abatishchev

Reputation: 100348

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

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351668

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.

Upvotes: 0

Brian Genisio
Brian Genisio

Reputation: 48157

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();
}

Upvotes: 0

Lennaert
Lennaert

Reputation: 2465

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?

Upvotes: 4

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422202

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.

Upvotes: 1

Fabian Vilers
Fabian Vilers

Reputation: 2992

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.

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115857

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.

Upvotes: 5

Related Questions