为什么 DAO 必须是接口或抽象类? [英] Why does a DAO have to be an interface or abstract class?

查看:92
本文介绍了为什么 DAO 必须是接口或抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 RoomDatabase 幕后发生的事情,它要求 DAO 是一个接口或一个抽象类.我已经搜索了很长时间,但所有文章和文档都只解释了方法,而不是原因.

I want to understand what is happening behind the scenes in a RoomDatabase, that it requires the DAO to be either an interface or an abstract class. I've been searching for quite a while, but all articles and documentation only explain the how, not the why.

推荐答案

不只是 RoomRetrofit 和其他库也使用这种模式,它被称为编程接口.而不是仅仅创建一个具体的实现,您只需指定您想要做的事情,他们就会为您提供一个将按照您的要求运行的实现.

It is not just the Room, Retrofit and other libraries use this pattern too, it is called Programming to an Interface. Instead of just creating a concrete implementation you just specify the stuff you want to do and they provide you with an implementation that will behave as you requested.

要进一步学习,您可以查看这篇文章:https://tuhrig.de/programming-to-an-interface/

For further study you can check this article: https://tuhrig.de/programming-to-an-interface/

这篇关于为什么 DAO 必须是接口或抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆