(Programming on v / s interface working with a specific class) when there is only one specific class

In an OO component, when you have only one implementation available for a class, and this class is not “published” to other components, is it still advisable to have an interface and work with the interface instead?

I am fully aware of the principle of designing "programming for the interface", and I also use it widely.

Recently, I have been observing that most of the time no other implementation is required (although this is possible and makes sense). As a result, always working with interfaces, the application code will have a sufficient number of interfaces with one implementation for each, and the interface seems like overhead.

Instead, is it preferable to work with a specific class and enter an interface only when a second implementation is required? In any case, currently extracting an interface using the IDE is a breeze. And when the new interface is introduced, references to the old concrete class can be changed to use the new interface instead.

What do you think?

+5
source share
6 answers

As far as I know, there are three situations when the interface is justified:

  • This is part of the architecture (I admit that this is not very clearly defined).
  • You have more than 1 implementation.
  • This is an open interface.

Do not be afraid to use specific classes. Do not mechanically create an interface for each individual class.

0

, , , . , , , .

, , . , /? , , , .

, , - .

+13

- , , .

, , API. , API API, .

+6

, . . , . , YAGNI

+3

( API) , "" , "" .

0

: . , . , , : () . , , , !

0

All Articles