I am struggling with applying the "programming to an interface" guideline because I can't seem to decide in which situations it is necessary and in which ones it's overkill (or even counter productive).
I would like to know of any heuristics or rules that you use in your projects to decide if you need to create a new interface and use it instead of an specific class.
Just to clarify, note that the question is not about the benefits of this guideline or the importance of following it. It's about some basic rules you use to know when to use it and when not to.
interface
orabstract
class
? Or do you simply mean the API of the implementing class? – Robert Harvey Jun 27 '16 at 00:05interface
. It's a sensible thing to do since, if you're going to go to all the trouble to write a class for its OO benefits like encapsulation, then you might as well respect the API it offers. Does that make sense? – Robert Harvey Jun 27 '16 at 00:07interface
when you want the benefits its offers. The primary benefit of aninterface
is to establish an API by which you can write more than one implementation. This allows you to do things like substitute a mock object or double for testing purposes. The mock object is your alternate implementation. – Robert Harvey Jun 27 '16 at 00:09