Programming oriented towards interface and not for implementation, why?


What are the main reasons (in practice) that lead developers to apply the practice of developing interface-oriented rather than implementation?

asked by anonymous 11.09.2015 / 21:03

1 answer


Because interfaces are just contracts of what the object has or is able to do. Then you can use any object as long as the contract is guaranteed.

Having just the contract you can get better:

  • Maneuverability - changes are more isolated, you do not have to change everything that accepted a particular class to accept another needed. Certain details are no longer important and you can change the implementation without breaking the application.
  • Extensibility - Allows new implementations to be made without changing everything expected of a given object, so certain behaviors become more generic and can manipulate objects that it does not know as long as it contains the expected contract.
  • Testability - It's easy to replace a real production object with a fake one that makes testing easier.

With the interface you can reduce the coupling .

It helps in encapsulation which is more than having a few private members. Prioritizing the use of the interface the code clearly says only what it needs there, and the languages usually prevent access to other members not present in the declared interface even though you know that these members exist in the object.

In some cases using interface is the same as using abstract classes.

There is an answer that shows in practice how important it is to be as generic as possible in the type of object that want and as specific as possible what to do with this object.

This facilitates a number of design patterns , especially the dependency injection and control inversion .

11.09.2015 / 21:45