POJO(Plain Old Java Object)和 DTO(Data Transfer Object)有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1425302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between POJO (Plain Old Java Object) and DTO (Data Transfer Object)?
提问by d1ck50n
I cannot find difference between them. Does anyone know how to differentiate them?
我找不到它们之间的区别。有谁知道如何区分它们?
回答by Noon Silk
POJO = Plain Old Java Object
POJO = 普通旧 Java 对象
DTO = Data Transfer Object
DTO = 数据传输对象
-- Edit
- 编辑
Well, this is assuming you don't know what the acronyms mean. A Pojo is just an object that is free from any sort of inheritance chain. A DTO exists in your data model, so probably follows a strict chain relating it to a given entity.
好吧,这是假设您不知道首字母缩略词的含义。Pojo 只是一个不受任何继承链影响的对象。DTO 存在于您的数据模型中,因此可能遵循将其与给定实体相关联的严格链。
回答by Jon
A POJOis just a simple Java object, the acronym is used to emphasize that it really is nothing special.
一个POJO只是一个简单的Java对象,缩写,是用来强调,这真的是没什么特别的。
A DTOis a Data Transfer Object which is used to encapsulate data that is transferred over a connection between layers or subsystems. See the wikipedia article, it's also a Core J2EE pattern (http://www.oracle.com/technetwork/java/transferobject-139757.html).
甲DTO是用于到被过度层或子系统之间的连接传送封装数据的数据传输对象。参见维基百科文章,它也是一个核心 J2EE 模式(http://www.oracle.com/technetwork/java/transferobject-139757.html)。
回答by user10398
DTO (Data transfer object): Is a Core J2EE design pattern used for transferring data within the system.DTO Pattern
DTO(数据传输对象):是一种核心 J2EE 设计模式,用于在系统内传输数据。DTO模式
POJO (Plain Old Java Object): It is just an acronym people use for suggesting that is a simple java object (which nowadays is heavily annotated for doing some meaning full work).
POJO(Plain Old Java Object):这只是人们用来暗示它是一个简单的 Java 对象的首字母缩写词(如今,它被大量注释以完成一些有意义的完整工作)。
回答by SingleShot
POJO or "Plain Old Java Object" is a name used to describe "ordinary" Java objects, as opposed to EJBs (originally) or anything considered "heavy" with dependencies on other technologies.
POJO 或“Plain Old Java Object”是一个名称,用于描述“普通”Java 对象,而不是 EJB(最初)或任何被认为“重”且依赖于其他技术的东西。
DTO or "Data Transfer Object" is an object for... well... transferring data, usually between your "business" classes and persistence layer. It typically is a behavior-less class much like a C-style struct. They are an outdated concept.
DTO 或“数据传输对象”是一个用于……嗯……传输数据的对象,通常在您的“业务”类和持久层之间。它通常是一个无行为的类,很像 C 风格的结构。它们是一个过时的概念。
回答by Abhijeet Kashnia
A POJO can have behavior. The book POJOs in Action details use of POJOS for application development. DTOs are data containers that help transfer data from one layer to another. DTOs aren't supposed to contain any behavior.
POJO 可以有行为。POJOs in Action 一书详细介绍了如何使用 POJOS 进行应用程序开发。DTO 是数据容器,可帮助将数据从一层传输到另一层。DTO 不应该包含任何行为。
回答by Paulo Merson
All DTOs are POJOs, but not all POJOs are DTOs. An example of POJO that is not a DTO is a business class that contains state and behavior (business logic).
所有 DTO 都是 POJO,但并非所有 POJO 都是 DTO。不是 DTO 的 POJO 示例是包含状态和行为(业务逻辑)的业务类。
回答by user3016087
DTO is pojo, but pojo is not dto, because pojo can have more behavior but DTO just basically no behavior
DTO 是 pojo,但 pojo 不是 dto,因为 pojo 可以有更多行为,但 DTO 基本上没有行为
Oracle document has clear description.
Oracle 文档有明确的描述。
回答by Hardzsi
I could understand the difference between POJO and DTO from this sentence of DTO's wiki:
我可以从DTO's wiki 的这句话中理解 POJO 和 DTO 之间的区别:
DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.
DTO 是简单的对象,不应包含任何业务逻辑,但可能包含用于通过线路传输数据的序列化和反序列化机制。
Also, DTO is perfectly visualized and described in detail in Martin Fowler's Catalog of Patterns of Enterprise Application Architecture
此外,DTO 在 Martin Fowler 的企业应用程序架构模式目录中得到了完美的可视化和详细描述