crv
crv

Reputation: 3024

Best practices when using an interface

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.

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?

Upvotes: 5

Views: 4116

Answers (2)

Alex J
Alex J

Reputation: 10205

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.

Upvotes: 1

jgauffin
jgauffin

Reputation: 101176

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.

Upvotes: 14

Related Questions