为什么要在Dao设计模式或其他设计模式中使用界面 [英] Why use interface in Dao design pattern or other design patterns

查看:121
本文介绍了为什么要在Dao设计模式或其他设计模式中使用界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据访问对象模式或DAO模式用于将低级数据访问API或操作从高级业务中分离出来。

服务。以下是数据访问对象模式的参与者。



数据访问对象接口 - 该接口定义了要在模型对象上执行的标准操作。



数据访问对象具体类 - 此类实现上述接口。该类负责从数据源获取数据,该数据源可以是数据库/ xml或任何其他存储机制。



模型对象或值对象 - 此对象是简单的POJO,包含get / set方法来存储使用DAO类检索的数据。

为什么我们需要一个INTERFACE时,我们有一个具体的类,为什么我们不能直接使用它?这可能是一个天真的问题,但请帮助我明确这件事情。不仅在DAO设计模式中,而且在其他设计模式中,使用INTERFACE也有点混乱。我同意这与代码可重用性和减少耦合有关。但任何人都可以请进一步解释一下。

解决方案


不仅在DAO设计模式中,而且在其他设计模式中也使用
INTERFACE有点令人困惑。

Interfaces 是Java中最常用的概念之一。让我以一个例子来解释一下:假设你设计了一个汽车GPS装置,它可以查看地图并自动将汽车转向地图所示的方向。该GPS设备可用于诸如奔驰,菲亚特等许多汽车。对于每辆汽车,根据汽车系统的实施情况,左右转向的机制可能不同。所以,这些功能应该由汽车制造商编写,并且这些方法放入一个界面中,由汽车制造商根据他的汽车的实施情况实施。该界面仅包含一组由汽车制造商定义的功能声明(在这种情况下)。得到它了?

要了解更多关于接口及其为什么有用的信息,请阅读

在下面的答案中指出的许多其他好处中,您可以创建许多DAO类用于不同的数据结构(德比数据库,巨大的堆栈等),它们实现了DAO接口。好处是,每个类都可以存储在DAO接口变量中,它的名为多态性

Please see below components of a Dao design pattern:

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.

Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).

Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism.

Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.

Why do we need an INTERFACE when we have a concrete class and why can't we use it directly? This might be a naive question but please help me get this thing clear. Not only in DAO design pattern but in other design patterns also use of INTERFACE is bit confusing. I agree this is related to code reusabilty and reduced coupling. But can anyone please explain a bit further.

解决方案

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 henve 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 usefull, 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.

这篇关于为什么要在Dao设计模式或其他设计模式中使用界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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