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)
-
No, it's not "more generic interface inheriting from a specific interface".
IMeasurableFoo, having only several properties, is the generic interface, whereasIFoois more specific, since it refines the knowledge about theIMeasurableFoo. ThinkIListinheriting fromIEnumerable.ooo : the reason i say this is that Bird would not inherit from FlyingBird? it would be the other way around -
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.
-
No, I don't think there's something wrong with it. Probably, it's just the name of
IMeasurableFoothat's misleading since it's just measurable and it's not a completeFoo. -
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?
Patrick McDonald : Then IMeasurable would be akin to IDisposable, +1 -
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(); } -
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.
-
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
0 comments:
Post a Comment