1

I have recently reviewed a few spring projects. I saw some interface which created for one class in dao and service layer of some projects.

like this:

     public interface EmployeeDao(){
         //some methods declaration
     }
     public class EmployeeDaoImp implements EmployeeDao (){
         // methods overriding
     }



     public interface CompanyDao(){
       //some methods declaration
     }
     public class CompanyDaoImp implements CompanyDao (){ 
        // methods overriding
     }

there is no need to Polymorphism in this case.

why we need to these interfaces?,what is advantage of these?.

I hope I could express myself.

Samet Baskıcı
  • 1,090
  • 3
  • 13
  • 26
  • see https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – Scary Wombat Jun 26 '17 at 01:12
  • The advantage would generally be that if you change the underlying database -- for instance, to use a NoSQL database instead of MySQ -- then you can change the impl without changing other parts of your code. Even if you only have one impl in production, being able to mock one out in unit tests is useful, and interfaces can be better for that than concrete classes. – yshavit Jun 26 '17 at 01:19
  • The other advantage is that you can have a 'base' interface for multiple derived objects and use it in different contexts, i.e. you can create a list of EmployeeDao which would be separate from the CompanyDao. Or you can use 'instanceof' to sort the objects, or whatever. Even if the interfaces have no functions in them. – Serge Jun 26 '17 at 02:08
  • 1
    oops... I think I may have read your question differently... is your question about "why empty interfaces" or "why interfaces with only 1 implementation" ? – Adrian Shum Jun 26 '17 at 02:30
  • @Serge If the interfaces have no methods, an annotation with runtime retention is usually better. – yshavit Jun 26 '17 at 03:41
  • interfaces with only 1 implementation so why we need it? – Samet Baskıcı Jun 26 '17 at 05:37
  • 1
    Do you need it no you don't. Advantages, testing an interface is easier to mock then a class. During runtime using interfaces you can use JDK dynamic proxies instead of cglib based class proxies (so you can rely on JDK native stuff instead of additional libraries). Also with the interface you can hide the actual implementation, using a class users of your class can see the implementation details. I tend to make the actual implementation package protected (this way you prevent leaking the actual implementation). – M. Deinum Jun 26 '17 at 06:19
  • @Charizard_11 Fix your post if it is your question. And, "Polymorphism" does not means you have multiple implementations. Please ask in more accurate wordings – Adrian Shum Jun 26 '17 at 06:59
  • @yshavit It depends on your usage model. With annotations you cannot use typed list. Yes, you can use them to figure out types, but the syntax is more complicated and less understood. – Serge Jun 26 '17 at 10:21

1 Answers1

-1

Spring uses interfaces a lot because they follow the design principle "Program to an interface not to an implementation"

See Programming to an interface

ipper
  • 624
  • 4
  • 13