Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what reasons are there to use interfaces (Java EE or Spring and JPA)

Most of the J2EE(Spring and JPA) classes are designed with interfaces. Except for inheritance, are there any technical reasons for this? Like dynamic proxy or AOP, I need more technical details about this

ex

public interface UserDAO {
   void delete();
   void update();
   void save();
   List<User> get();
}

public class UserDAOImpl implements UserDAO {
   public void delete(){}
   public void update(){}
   public void save(){}
   public List<User> get(){}
}
like image 453
Arun Avatar asked Jan 17 '12 12:01

Arun


People also ask

Why are interfaces used in spring?

Using Interfaces allows your classes to extend from some other classes if required. Your Interfaces can have multiple implementations and you can switch between any of them without changing the client code.

What are the advantages of using spring than Java EE?

Advantages of SpringUses POJO, don't need an enterprise container like an application server. Provides Modularity to developers. Consistency of Transaction Management. Well- Designed Web Framework.

Do I need an interface with spring boot?

So, if you ask me whether you should use an interface for your services, my answer would be no. The only exception is if you're either trying to use inversion of control, or you have multiple implementations to take care of. You might think, wouldn't it be better to create an interface, just in case?

WHAT IS interface in Java Spring?

In Java, an interface is an abstract type that contains a collection of methods and constant variables. It is one of the core concepts in Java and is used to achieve abstraction, polymorphism and multiple inheritances.


2 Answers

There are 3 main reasons, IMO:

First reason: proxies.

If you ask Spring for the bean of type UserDAO, it will in fact return a proxy encapsulating the actual UserDAOImpl instance. This allows it to demarcate transactions, verify security authorization, log accesses, compute statistics, etc. It's possible to do it without an interface, but then byte-code manipulation is needed.

Second reasons: testability.

When unit-testing a business service which uses a UserDAO, you typically inject a mock UserDAO implementation. Once again, this is easier to do when UserDAO is an interface. It's possible with a concrete class, but it has not always been, and it's still easier with an interface

Third reason: decoupling.

By using an interface, you have a place where you define the real contract of the DAO for its clients. Sure, it needs a setDataSource() method in the concrete implementation, but clients don't care about that. All they need is set of data-access methods offered by the DAO. By separating the interface and the concrete implementation, you make sure that the client doesn't rely on implementation details of the DAO.

like image 88
JB Nizet Avatar answered Oct 26 '22 18:10

JB Nizet


  1. Interfaces for one are contracts as I see them . For example , a set of software engineers(Implementation classes) may have a specific contract with a company(Interface) . The company may choose to switch between the engineers from time to time based on the project needs . Since they come under the same contract and follow the same rules , switching is easier than bringing in a resource from outside (writing a new class) every time the project needs change . You just have to change the configurations to switch the implementation classes .

  2. Interfaces are clean and is a one point access to the rules which the classes implement .

Links

  1. spring and interfaces
  2. What does it mean to "program to an interface"?
  3. http://www.artima.com/lejava/articles/designprinciples.html
like image 36
Aravind A Avatar answered Oct 26 '22 17:10

Aravind A