见过一些类似的问题:
JavaBean和POJO之间的区别是什么? POJO(普通旧Java对象)和DTO(数据传输对象)之间的区别是什么?
你能告诉我它们在什么语境中使用吗?或者它们的目的是什么?
见过一些类似的问题:
JavaBean和POJO之间的区别是什么? POJO(普通旧Java对象)和DTO(数据传输对象)之间的区别是什么?
你能告诉我它们在什么语境中使用吗?或者它们的目的是什么?
当前回答
值对象:当需要根据对象的值来衡量对象是否相等时使用。 数据传输对象:将具有多个属性的数据一次跨层从客户端传递到服务器,避免多次调用远程服务器。 普通旧Java对象:它就像一个简单的类,有属性,公共无参数构造函数。正如我们为JPA实体声明的那样。
difference-between-value-object-pattern-and-data-transfer-pattern
其他回答
POJO: 它是一个java文件(类),不扩展或实现任何其他java文件(类)。
豆: 它是一个java文件(类),其中所有变量都是私有的,方法是公共的,并且使用适当的getter和setter来访问变量。
普通类: 它是一个java文件(类),可能由公共/私有/默认/受保护的变量组成,它可能扩展也可能不实现另一个java文件(类)。
Java bean与ejb不是一回事。
Java 1.0中的JavaBeans规范是Sun试图允许在看起来像VB的IDE中操作Java对象的尝试。对于符合“Java Beans”条件的对象,制定了一些规则:
默认构造函数 私有数据成员的getter和setter遵循适当的命名约定 可序列化的 也许还有一些我忘了的。
ejb是后来出现的。它们结合了分布式组件和事务模型,运行在管理线程、池、生命周期和提供服务的容器中。它们与Java bean相去甚远。
dto是在Java上下文中出现的,因为人们发现EJB 1.0规范与数据库过于“健谈”。人们不需要为每个数据元素进行往返,而是将它们批量打包到Java bean中并四处运送。
pojo是对ejb的一种反应。
javabean
JavaBean是一个遵循Sun定义的JavaBean约定的类。维基百科对javabean有一个很好的总结:
JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods. In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behavior. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans. The required conventions are: The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks. The class properties must be accessible using get, set, and other methods (so-called accessor methods and mutator methods), following a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. The class should be serializable. This allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion that is independent of the VM and platform. Because these requirements are largely expressed as conventions rather than by implementing interfaces, some developers view JavaBeans as Plain Old Java Objects that follow specific naming conventions.
POJO
普通旧Java对象或POJO最初是用来指定简单的轻量级Java对象的术语,它没有实现任何javax。ejb接口,而不是重量级的ejb 2。x(特别是实体bean,无状态会话bean在我看来并不是那么糟糕)。今天,这个词被用于任何简单的物体,没有额外的东西。维基百科在定义POJO方面做得很好:
POJO is an acronym for Plain Old Java Object. The name is used to emphasize that the object in question is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean (especially before EJB 3). The term was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000: "We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely." The term continues the pattern of older terms for technologies that do not use fancy new features, such as POTS (Plain Old Telephone Service) in telephony, and PODS (Plain Old Data Structures) that are defined in C++ but use only C language features, and POD (Plain Old Documentation) in Perl. The term has most likely gained widespread acceptance because of the need for a common and easily understood term that contrasts with complicated object frameworks. A JavaBean is a POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods. An Enterprise JavaBean is not a single class but an entire component model (again, EJB 3 reduces the complexity of Enterprise JavaBeans). As designs using POJOs have become more commonly-used, systems have arisen that give POJOs some of the functionality used in frameworks and more choice about which areas of functionality are actually needed. Hibernate and Spring are examples.
值对象
值对象或VO是一个对象,例如java.lang.Integer,它保存值(因此是值对象)。对于更正式的定义,我经常参考Martin Fowler对Value Object的描述:
In Patterns of Enterprise Application Architecture I described Value Object as a small object such as a Money or date range object. Their key property is that they follow value semantics rather than reference semantics. You can usually tell them because their notion of equality isn't based on identity, instead two value objects are equal if all their fields are equal. Although all fields are equal, you don't need to compare all fields if a subset is unique - for example currency codes for currency objects are enough to test equality. A general heuristic is that value objects should be entirely immutable. If you want to change a value object you should replace the object with a new one and not be allowed to update the values of the value object itself - updatable value objects lead to aliasing problems. Early J2EE literature used the term value object to describe a different notion, what I call a Data Transfer Object. They have since changed their usage and use the term Transfer Object instead. You can find some more good material on value objects on the wiki and by Dirk Riehle.
数据传输对象
数据传输对象(DTO)是EJB引入的一种(反)模式。与其在ejb上执行许多远程调用,其思想是将数据封装在一个可以通过网络传输的值对象中:数据传输对象。维基百科对数据传输对象有一个很好的定义:
Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems. DTOs are often used in conjunction with data access objects to retrieve data from a database. The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behaviour except for storage and retrieval of its own data (accessors and mutators). In a traditional EJB architecture, DTOs serve dual purposes: first, they work around the problem that entity beans are not serializable; second, they implicitly define an assembly phase where all data to be used by the view is fetched and marshalled into the DTOs before returning control to the presentation tier.
因此,对于许多人来说,dto和VOs是同一个东西(但是Fowler用VOs来表示我们看到的其他东西)。大多数时候,它们遵循javabean约定,因此也是javabean。而且都是pojo。
值对象:当需要根据对象的值来衡量对象是否相等时使用。 数据传输对象:将具有多个属性的数据一次跨层从客户端传递到服务器,避免多次调用远程服务器。 普通旧Java对象:它就像一个简单的类,有属性,公共无参数构造函数。正如我们为JPA实体声明的那样。
difference-between-value-object-pattern-and-data-transfer-pattern
基本上,
DTO:“数据传输对象”可以在软件架构的不同层之间传输。
VO:“值对象”包含一个对象,如Integer,Money等。
POJO:普通的旧Java对象,不是一个特殊的对象。
Java Beans:要求Java类是可序列化的,有一个无参数的构造函数和每个字段的getter和setter