12

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(){}
}
Archmede
  • 1,592
  • 2
  • 20
  • 37
Arun
  • 1,167
  • 18
  • 37
  • possible duplicate of [spring and interfaces](http://stackoverflow.com/questions/256255/spring-and-interfaces) – tolitius Jan 17 '12 at 22:29

3 Answers3

29

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thanks. +1 for first reason (It's possible to do it without an interface, but then byte-code manipulation is needed) – Arun Jan 17 '12 at 13:28
  • 1
    You mean +1 for all three right? Unit testing is so much easier when you code to interfaces. – Alex Barnes Jan 17 '12 at 13:40
  • 2
    It depends. why do you need an interface if there is only one implementation and you are not creating a framework or library. – James Jan 07 '14 at 08:40
  • 2
    @James Nowadays, with Mockito and good CGLib support in Spring, interfaces are not really needed anymore. But with EasyMock (which is not as smart as Mockito to mock concrete classes) having interfaces really helped. – JB Nizet Jan 07 '14 at 10:03
  • 1
    Why is byte-code manipulation viewed as something not desired? – A. Alencar Jun 23 '20 at 19:00
7
  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
Community
  • 1
  • 1
Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • 1
    References to existing questions w/o further discussion should almost always be either a close/duplicate vote, or a comment, IMO. – Dave Newton Jan 17 '12 at 13:01
  • @Dave Newton - pointing people to the right direction sometimes consumes a bit of time which I feel should be 'awarded' . An upvote says 'This answer is useful ' and is chosen by those who find it useful which IMO is acceptable . – Aravind A Jan 17 '12 at 13:42
  • I'm fine with you believing whatever you want; "Just links" are not recommended SO answers as per [here](http://meta.stackexchange.com/questions/118582/what-is-an-acceptable-answer) and [here](http://meta.stackexchange.com/questions/118926/right-way-to-answer-a-question-with-just-a-link). – Dave Newton Jan 17 '12 at 14:05
  • @Dave Newton Thanks for pointing this out . I'll keep this in mind but this again is someone's opinion :) .Anyhow, thanks again for the your time . – Aravind A Jan 17 '12 at 14:34
  • If by "someone" you mean "the people that are basically in charge of the site" (continue searching meta; you'll find the same info repeatedly), yep--all just opinion. – Dave Newton Jan 17 '12 at 14:39
5

Because you have mentioned Spring explicite.

Spring AOP can be used in different configurations. The default one uses java dynamic proxies (java.lang.reflect.Proxy). This can be only applied to Interfaces

Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

@see Spring Reference Chapter 7.1.3 AOP Proxies @see Dynamic Proxy Classes

Ralph
  • 118,862
  • 56
  • 287
  • 383