-1

Why it is best practice to call method implementation of another layer (e.g. call from Service to DAO layer) through interface?

nilFi
  • 197
  • 2
  • 18

3 Answers3

2

Primary because then you can change the implementation of that layer without altering the client code.

Have a look at SOLID

tibtof
  • 7,857
  • 1
  • 32
  • 49
2

Its n-tier architecture where n=1,2..n. The purpose of N tier architecture is to insulate the different layers of the application from each other. Lets take an example of web application. the GUI client(JSP/Thick client/MobileAPP etc) doesn't know how the server is working internally, and the server doesn't know how the database server works internally etc. They just communicate via standard interfaces. If you have change DB to JMS or to webservice, all you need to change is impl* classes without changes interfaces.

Separating application components into separate tiers increases the maintainability and scalability of the application. It does this by enabling easier adoption of new technologies that can be applied to a single tier without the requirement to redesign the whole solution. In addition, n-tier applications typically store sensitive information in the middle-tier, which maintains isolation from the presentation tier.

This also helps in testing, maintaining and debugging easy.

Suggest you read here and here

Community
  • 1
  • 1
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
1

The simple practice is abstraction .. you hide the actual implementation and just expose the definition...for example List is an interface and ArrayList and LinkedList are the implementations .... In the future you may need to change the implementation and with interface in place you will not change the client code ... you will change the implementing class without any effect on client

kanishka vatsa
  • 2,074
  • 18
  • 8