9

In our project architecture we are using a classic MVC pattern including a classic service layer (opening the transaction and calling the DAO layer).

For each service we have an implementation and his interface. But to be honest, I'm pretty sure that for one service and his interface, we will never have more than one implementation. So ok maybe it's more clear to have the public method declared in the interface helping to know what the service does, but an interface is used to have multiple implementation and if we know that we won't have more than one implementation, should we keep them?

Emilien Brigand
  • 9,943
  • 8
  • 32
  • 37

2 Answers2

6

From the documentation:

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.

If you know that you will only have one implementation, the implementation itself will define the contract, so you can remove the interfaces.

But writing an interface could help you to better define the contract and also, you could need at a given point to write a mock for the service, in such a case you would benefit from the use of interfaces.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • I agree it can help for unit tests, but unit tests should not drive the dev isn't it? and I didn't mention it but we are using Spring, and to not use interfaces for components can bring issues sometimes (because of the proxying system used by spring ?)... – Emilien Brigand Feb 24 '15 at 11:08
  • I am talking about mocking your services (returning a mock list of entities just to see if your view shows the collection properly). In your case, to avoid messing with the Spring proxying I would use interfaces for sure. Take a look at this: http://stackoverflow.com/questions/11528061/i-want-to-define-a-spring-bean-class-with-no-interface – antonio Feb 24 '15 at 11:15
6

i think this is a good approach for keeping interfaces.

reasons: 1. say you want to write junits for the same with a different implementations ex. inspite of getting data from database you want to get data from a separate datasource then a different implementation will suffice.

anurag gupta
  • 379
  • 1
  • 5