4

Many times when designing interfaces I keep running into the same situation. The situation is where certain implementations using an interface require particular parameters in the interface while others do not.

  • What is the best practice when designing an interface?
  • Is it OK to have certain implementations that implement the interface but not use all the parameters?

Or in these situations should I just be taking in a list (some structure) of parameters and deal with that list accordingly in each implementation?

jgauffin
  • 99,844
  • 45
  • 235
  • 372
crv
  • 3,024
  • 4
  • 27
  • 31

2 Answers2

13

No it's not OK. It breaks Liskovs Substituion Principle.

Sounds to me that your interfaces are trying to do too much. Either use interface inheritance or split the interface into multiple ones. Do note that it's better having many small interfaces than one large. Classes can still implement all of those.

Interfaces, like classes, should follow SRP (Single Responsibility Principle). imho it's much much more important that interfaces do so since they force a design upon the rest of your application.

I also tend to try to avoid adding properties as much as possible from interfaces.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • I agree with you on LSP and SRP, but as long as the property is a simple property (such as IsEnabled) I personally have no problems with those kind of properties in an interface. I would be more inclined to avoid more data-ish properties, though. – James Michael Hare Apr 21 '11 at 14:03
  • Yep. I was a bit unclear there. I do agree with you about the properties. Thanks for clarifying that for me ;) – jgauffin Apr 21 '11 at 17:48
  • As I suspected I've been mislead. I've run into a bunch of situations where someone has created an interface and used it inconsistently. When asking the culprits they tried to say that it was "OK". Thanks for clearing this up. – crv Apr 21 '11 at 19:51
1

It's ok in some cases. It doesn't really matter what you actually do with the parameters in the implementation as long as it satisfies the contract that the interface promises to uphold.

But you should reconsider whether you don't actually want a more specific interface for some things that need those parameters. In your abstraction stack, having a "lower" interface need "higher" parameters is a break of encapsulation.

Alex J
  • 9,905
  • 6
  • 36
  • 46