OOP Good Practices: Coding to the interface

introdev.io
Javarevisited
Published in
4 min readOct 12, 2017

--

“Coding to the interface,” “coding against the interface,” “interface-based programming” and more… are just a few names given to a, in my opinion, incredibly useful and scalable technique for writing systems that are highly dependent on changes.

It came to my mind to talk about this when a few days ago a colleague with whom I’m working on a project told me that he was having a tough time understanding the code I wrote; the big question arose “why so many interfaces?” I felt somewhat identified because even for me (that wrote all that code) was difficult at times find something between all those files. But the reason for it? Simple: “Coding to interfaces, not implementation.”

Coding to interfaces is a technique to write classes based on an interface; interface that defines what the behavior of the object should be. It involves creating an interface first, defining its methods and then creating the actual class with the implementation. At first, it looks like over coding, but, there are two main reasons that why I decided to go for it: Test Driven Development and Flexibility.

On the testing side, is absolutely needed. TDD is about creating the tests for the functionality to achieve, to later develop the code required to pass the tests. As there is no implementation of the code yet, using Interfaces to Mock objects is…

--

--

introdev.io
Javarevisited

Eager to learn and share knowledge; Writing about programming in Java and JavaScript; Software Architecture And Mental Health.