简体   繁体   中英

DAO interface, class and factories

So I am currently learning about the Dao in Java. I am finding it hard to grasp the concepts for the most part. I believe the Dao interface is for listing out the methods that are going to help you CRUD the database. As for the Dao concrete class.. that is just a class that does the actual implementation of the Dao interface. But I do not understand what the Dao Factory purpose is. Any input would be appreciated.

See: https://www.baeldung.com/java-dao-pattern . The Dao is an interface. To use it, you would have to implement it or better yet include a Jpa provider in your project that already has implemented it. The factory allows different implementations of the interface without the application knowing.

For example, you might use an Oracle database today and tomorrow switch to MySql. You can just change your driver (a different jar) and some properties. The factory will then get the proper implementation of the Dao for the configured database. The code to save and find entities from the database doesn't need to know anything changed.

That said in the wild this doesn't usually go this smoothly for decent size apps. There are a lot of ways to sneak dependencies on a particular technology into an app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM