• Post Reply Bookmark Topic Watch Topic
  • New Topic

why should we use interFaces in service and DAO layers  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyOne,

when i am developing my projcet , in service and DAO layers we used to write interfaces and implement them , why should we do like that,
example for UserService , first we used to write IUserService interface and declare the methods in it .and implement that service as UserService .
why should we fallow that procedure.


Thanks
raj
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One good reason is that it makes it easy to write mock implementations for testing your application.

Say you want to test module X that uses the UserService. When you write test code, you'll want it to not use the real UserService, but a special test version of UserService that returns some predefined test data. So, in your test, you pass your own mock implementation of IUserService instead of the real UserService.

Also, it makes it easier to in the future extend the system with a different implementation of IUserService. Maybe you now have a UserService that reads information about users from a database. In the future you might, for example, also want to have a UserService that gets information from LDAP. That would just be another implementation of IUserService. Since the rest of the application uses the UserService only through the interface, it will be easy to swap out one implementation for the other.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!