Why use interface in Dao design pattern or other design patterns Why use interface in Dao design pattern or other design patterns spring spring

Why use interface in Dao design pattern or other design patterns


Not only in DAO design pattern but in other design patterns also use of INTERFACE is bit confusing.

Interfaces is one of the best used concepts in Java. Let me explain this with an example: Say you designed a GPS device for car which looks into the map and automatically turns the car to the direction as seen in the map. This GPS device can be used in many cars like benz, fiat, etc. For each car, the mechanism of turning the left or right may differ depending on the implementation of the car system. So,these functions should be written by the car manufacturer and hence these methods are put in a interface, which is implemented by the car manufacture as per his car's implementation. The interface includes only a set of function declarations which are to be defined by the car manufacturer(In this case). Got it?

To learn more about interfaces and why they are useful, read this article.

My question is: Why do we need an INTERFACE when we have a concrete class and why can't we use it directly.

Among many other benefits that were pointed out in answers below, you can create many DAO classes for different data structers (derby db, huge stacks etc.), that implements DAO interface. The benefit is, that every class can be stored in DAO interface variable, its called polymorphism.


Actually its not necessary to have an interface when you have only one implementation. But there are certain situation where it is very practical that you dont have a depencecy to the concrete class:

  • Testing your service that calls the DAO: You can write a mock DAOthat behaves just as you need it in the test (e.g. simulat that thereis no DB connection, which is hard to reproduce automatically)

  • generate some Layers aoround your DAO. You could use AOP to generatecaching or transaction handling around your DAO methods. In this caseyou have an object that implements the DAOs interface but has nothingto do with the original implementation.

  • Switching the DB technology. If you switch from MySQL to DB2 you justneed to write another implementation of the interface and switch theMySQL DAO and the DB2 DAO

Therefore its a good practice to have an interface for you DAOs and Services.


My question is Why do we need an INTERFACE when we have a concrete class and why can't we use it directly.

It's simple abstraction. Suppose you are using Oracle Database as your database. So the concrete class will have logic to access(CRUD ops) the Oracle DB. Tomorrow if your license expires and you no longer want to use Oracle DB, instead you'll want to use MySQL. Now you'll have to rewrite the concrete class already mentioned and with that you'll have to rewrite the service layer too because by directly using the concrete class and it's methods you have a tight coupling between the service layer and data access layer. One should always design systems with loose coupling in mind.

If you were to use an interface instead of the concrete class, the service layer and data access layer have a contract of how to interact. So the service layer will not be impacted by the changes in the data layer because the contract has not changed and they can interact in the same old fashion.