使用接口编写DAO类 [英] Using interfaces for writing DAO classes

查看:79
本文介绍了使用接口编写DAO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的Web应用程序,它将使用一堆数据访问对象(DAO)类对数据进行CRUD操作。我知道当我有使用DAO类的外部用户/应用程序时,应该编写Java接口。但是,如果没有这种需要,您认为我还是应该编写接口?我将使用spring将DAO类注入到Spring Controller(我正在使用Spring MVC)类中。

I'm creating a new web application which will use a bunch of Data Access Object(DAO) classes for doing CRUD operations on the data. I know I should write java interfaces when I have external users/applications using my DAO classes. But if there is no such need do you think I should still write the interfaces? I'll be injecting DAO classes in to the Spring Controller(I'm using Spring MVC) classes using spring.

推荐答案

注意那:您应该始终尝试将接口与实现分开。

NOTE THAT : You should always try to separating the Interface from the Implementation. This will give you more control to the other layers, using this DAO layer.

但是,正如您所知,接口为您提供了更多的抽象性,并使代码
更具灵活性和更改适应性,因为您可以使用同一接口的不同
实现,而无需更改其客户端。
不过,如果您不认为代码会更改,或者(特别是)如果
认为抽象足够好,则不必使用接口

But, As you know an interface gives you more abstraction, and makes the code more flexible and resilient to changes, because you can use different implementations of the same interface without changing its client. Still, if you don't think your code will change, or (specially) if you think your abstraction is good enough, you don't necessarily have to use interfaces

换句话说:接口是好的,但是在为
创建接口之前,每个类都要考虑一下

In other words: interfaces are good, but before making an interface for every class think about it

这篇关于使用接口编写DAO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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