2

I have been doing OOP for a quite while and I got confused with usages of interfaces after an argument with a colleague few days before.

Basically, I have been using interfaces when I apply design patterns, especially when there are multiple classes implementing common features.

In my application, there is Hibernate layer and few Sevices classes as example UserService, CompanyService, etc. The question was whether we keep, separate interfaces also for each Service classes. Such as UserServiceContract, CompanyContract and etc.

My colleague argument was, there is no need to have interfaces.

I came across that in this tutorial also, the author has used interfaces. But there is no common interface that implements several classes only once. example interface implentation

The benefit of using the interfaces was in this situation, it improves the code structure. Yes, there is IDE features that show what methods available for a class. But, I still want to get your guys idea too about this.

Pablo
  • 652
  • 6
  • 21
newday
  • 3,842
  • 10
  • 54
  • 79

1 Answers1

4

When you are talking design patterns and best coding practices, needing is not what you should be asking yourself. You do not need most of what you do, at least not immediately. Of course you do not know whether you will need different implementations of that contract until you actually do. So this is not a question of what you need right now. This is a question of what you will wish you had later.

What you have seen in the link you posted is the D in SOLID: the Dependency Inversion Principle:

Depend on abstractions, not on implementations.

You are better safe than sorry, you know.

EDIT: Also, I would advise against sufixes for interfaces (or prefixes). If you will be using the interface instead of the implementation, make the interface's name clean. A common choice would be, in your case, UserService for the interface and UserServiceImpl for the implementation.

Pablo
  • 652
  • 6
  • 21
  • 1
    +1 for the "you will wish you had later". And just want to mention that the interfaces give you the ability to isolate your components from each other and this gives you very good advantage for unit testing your code – Wahid Bitar Dec 19 '15 at 20:20
  • ok, so, It is better if I had Interfaces, when I started the Userservice classes. But I have already completed the project but I think there is no point of going back and creating Interfaces. Is that correct? – newday Dec 19 '15 at 23:55
  • No project is ever finished, really. My advice is that, if you decide you want the DIP approach, the sooner, the better. Later on, it will be harder to make the necessary adjustments. – Pablo Dec 20 '15 at 00:11
  • To add to this, I would avoid use of the Abstract Factory Pattern until you _need_ it. I find this is among the absolute hardest of the GOF (Gang of Four) patterns to both Learn and Teach(I've spent _hours_ teaching fellow PhD students it). And Editors (Eclipse at least, and I think IDEA & Android Studio) make it hard when you use that pattern because they can't help you 'follow' the code to the 'implementation' as easily as they can when you are 'just using interfaces'. (Even when you only have 1 impl) – mawalker Dec 20 '15 at 03:51
  • thanks guys for the detail response. – newday Dec 20 '15 at 22:02