What does the term Plain Old Java Object (POJO) mean? I couldn't find anything explanatory enough.
Plain Old Java Object(POJO)这个术语是什么意思?我找不到足够的解释。
POJO's Wikipedia page says that POJO is an ordinary Java Object and not a special object. Now, what makes or what doesn't make and object special in Java?
POJO的*页面说POJO是一个普通的Java对象,而不是一个特殊的对象。现在,什么使得或者什么不能使对象在Java中变得特别?
The above page also says that a POJO should not have to extend prespecified classes, implement prespecified Interfaces or contain prespecified Annotations. Does that also mean that POJOs are not allowed to implement interfaces like Serializable
, Comparable
or classes like Applets or any other user-written Class/Interfaces?
上面的页面还说明POJO不应该扩展预先指定的类,实现预先指定的接口或包含预先指定的注释。这是否也意味着不允许POJO实现Serializable,Comparable或Applet之类的接口或任何其他用户编写的类/接口?
Also, does the above policy (no extending, no implementing) means that we are not allowed to use any external libraries?
此外,上述政策(没有扩展,没有实施)是否意味着我们不允许使用任何外部库?
Where exactly are POJOs used?
POJO究竟在哪里使用?
EDIT: To be more specific, am I allowed to extend/implement classes/interfaces that are part of the Java or any external libraries?
编辑:更具体地说,我是否允许扩展/实现作为Java或任何外部库的一部分的类/接口?
8 个解决方案
#1
55
Plain Old Java Object The name is used to emphasize that a given object is an ordinary Java Object, not a special object such as those defined by the EJB 2 framework.
Plain Old Java Object该名称用于强调给定对象是普通Java对象,而不是EJB 2框架定义的特殊对象。
class A {}
class B extends/implements C {}
class A {} class B扩展/实现C {}
Note: B is non POJO when C is kind of distributed framework class or ifc. e.g. javax.servlet.http.HttpServlet, javax.ejb.EntityBean or J2EE extn and not serializable/comparable. Since serializable/comparable are valid for POJO.
注意:当C是一种分布式框架类或ifc时,B是非POJO。例如javax.servlet.http.HttpServlet,javax.ejb.EntityBean或J2EE extn,不可序列化/可比较。由于可序列化/可比较对POJO有效。
Here A is simple object which is independent. B is a Special obj since B is extending/implementing C. So B object gets some more meaning from C and B is restrictive to follow the rules from C. and B is tightly coupled with distributed framework. Hence B object is not POJO from its definition.
这里A是一个独立的简单对象。 B是一个特殊的obj,因为B正在扩展/实现C.所以B对象从C获得更多的意义,B是限制性地遵循C的规则而B与分布式框架紧密耦合。因此B对象不是其定义中的POJO。
Code using class A object reference does not have to know anything about the type of it, and It can be used with many frameworks.
使用类A对象引用的代码不必知道它的类型,它可以与许多框架一起使用。
So a POJO should not have to 1) extend prespecified classes and 2) Implement prespecified interfaces.
所以POJO不应该1)扩展预先指定的类和2)实现预先指定的接口。
JavaBean is a example of POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.
JavaBean是可序列化的POJO示例,具有无参数构造函数,并允许使用遵循简单命名约定的getter和setter方法访问属性。
POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. It means it has the code for business logic but how this instance is created, Which service(EJB..) this object belongs to and what are its special characteristics( Stateful/Stateless) it has will be decided by the frameworks by using external xml file.
POJO完全专注于业务逻辑,并且不依赖于(企业)框架。这意味着它具有业务逻辑的代码但是如何创建该实例,该对象属于哪个服务(EJB ..)以及它具有的特殊特性(有状态/无状态)将由框架通过使用外部xml来决定文件。
Example 1: JAXB is the service to represent java object as XML; These java objects are simple and come up with default constructor getters and setters.
示例1:JAXB是将Java对象表示为XML的服务;这些java对象很简单,并提供了默认的构造函数getter和setter。
Example 2: Hibernate where simple java class will be used to represent a Table. columns will be its instances.
示例2:Hibernate,其中简单的java类将用于表示Table。列将是它的实例。
Example 3: REST services. In REST services we will have Service Layer and Dao Layer to perform some operations over DB. So Dao will have vendor specific queries and operations. Service Layer will be responsible to call Which DAO layer to perform DB operations. Create or Update API(methods) of DAO will be take POJOs as arguments, and update that POJOs and insert/update in to DB. These POJOs (Java class) will have only states(instance variables) of each column and its getters and setters.
示例3:REST服务。在REST服务中,我们将使用Service Layer和Dao Layer来对DB执行某些操作。所以Dao将有供应商特定的查询和操作。服务层将负责调用哪个DAO层来执行数据库操作。创建或更新DAO的API(方法)将POJO作为参数,并更新POJO并插入/更新到DB。这些POJO(Java类)将只包含每列的状态(实例变量)及其getter和setter。
In practice, some people find annotations elegant, while they see XML as verbose, ugly and hard to maintain, yet others find annotations pollute the POJO model. Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead or in addition to XML:
在实践中,有些人发现注释优雅,而他们认为XML冗长,丑陋且难以维护,而其他人则发现注释会污染POJO模型。因此,作为XML的替代方案,许多框架(例如Spring,EJB和JPA)允许使用注释来代替或者使用XML:
Advantages:
Decoupling the application code from the infrastructure frameworks is one of the many benefits of using POJOs. Using POJOs future proofs your application's business logic by decoupling it from volatile, constantly evolving infrastructure frameworks. Upgrading to a new version or switching to a different framework becomes easier and less risky. POJOs also make testing easier, which simplifies and accelerates development. Your business logic will be clearer and simpler because it won't be tangled with the infrastructure code
优点:将应用程序代码与基础架构框架分离是使用POJO的众多好处之一。使用POJO可以将应用程序的业务逻辑与不稳定,不断发展的基础架构框架分离,从而证明其应用程序的业务逻辑。升级到新版本或切换到不同的框架变得更容易,风险也更小。 POJO还使测试更容易,这简化并加速了开发。您的业务逻辑将更清晰,更简单,因为它不会与基础架构代码纠缠在一起
参考文献:wiki source2
#2
9
According to Martin Fowler, he and some others came up with it as a way to describe something which was a standard class as opposed to an EJB etc.
根据Martin Fowler的说法,他和其他一些人提出它是一种描述标准类而不是EJB等的方法。
#3
7
Usage of the term implies what it's supposed to tell you. If, for example, a dependency injection framework tells you that you can inject a POJO into any other POJO they want to say that you do not have to do anything special: there is no need to obey any contracts with your object, implement any interfaces or extend special classes. You can just use whatever you've already got.
该术语的使用意味着它应该告诉你的内容。例如,如果依赖注入框架告诉您可以将POJO注入任何其他POJO,他们想要说您不必执行任何特殊操作:不需要遵守与您的对象的任何契约,实现任何接口或延长特殊课程。你可以使用你已经拥有的任何东西。
UPDATE To give another example: while Hibernate can map any POJO (any object you created) to SQL tables, in Core Data (Objective C on the iPhone) your objects have to extend NSManagedObject in order for the system to be able to persist them to a database. In that sense, Core Data cannot work with any POJO (or rather POOCO=PlainOldObjectiveCObject) while Hibernate can. (I might not by 100% correct re Core Data since I just started picking it up. Any hints / corrections are welcome :-) ).
更新举另一个例子:虽然Hibernate可以将任何POJO(您创建的任何对象)映射到SQL表,但在Core Data(iPhone上的Objective C)中,您的对象必须扩展NSManagedObject,以便系统能够将它们持久化一个数据库。从这个意义上讲,Core Data不能与任何POJO(或者更确切地说是POOCO = PlainOldObjectiveCObject)一起工作,而Hibernate可以。 (我可能不会100%更正核心数据,因为我刚开始接受它。欢迎任何提示/更正:-))。
#4
6
Plain Old Java Object :)
普通的旧Java对象:)
Well, you make it sound like those are all terrible restrictions.
好吧,你听起来好像那些都是可怕的限制。
In the usual context where POJO is/are used, it's more like a benefit:
在使用POJO的通常情况下,它更像是一个好处:
It means that whatever library/API you're working with is perfectly willing to work with Java objects that haven't been doctored or manhandled in any way, i.e. you don't have to do anything special to get them to work.
这意味着您正在使用的任何库/ API都非常愿意使用未经过任何方式修改或处理过的Java对象,即您无需执行任何特殊操作即可使用它们。
For example, the XStream XML processor will (I think) happily serialize Java classes that don't implement the Serializable
interface. That's a plus! Many products that work with data objects used to force you to implement SomeProprietaryDataObject
or even extend an AbstractProprietaryDataObject
class. Many libraries will expect bean behavior, i.e. getters and setters.
例如,XStream XML处理器(我认为)将愉快地序列化未实现Serializable接口的Java类。这是一个加号!许多使用数据对象的产品用于强制您实现SomeProprietaryDataObject,甚至扩展AbstractProprietaryDataObject类。许多库都会期望bean的行为,即getter和setter。
Usually, whatever works with POJOs will also work with not-so-PO-JO's. So XStream will of course also serialize Serializable classes.
通常情况下,与POJO一起使用的任何东西都可以与不那么PO-JO一起使用。所以XStream当然也会序列化Serializable类。
#5
4
POJO is a Plain Old Java Object - as compared to something needing Enterprise Edition's (J2EE) stuff (beans etc...).
POJO是一个普通的旧Java对象 - 与需要企业版(J2EE)的东西(bean等)相比。
POJO is not really a hard-and-fast definition, and more of a hand-wavy way of describing "normal" non-enterprise Java Objects. Whether using an external library or framework makes an object POJO or not is kind of in the eye of the beholder, largely depending on WHAT library/framework, although I'd venture to guess that a framework would make something less of a POJO
POJO并不是一个非常快速的定义,更像是一种描述“普通”非企业Java对象的手工波形方式。无论是使用外部库还是框架使POJO成为对象,都会在旁观者看来,主要依赖于WHAT库/框架,尽管我冒昧地猜测框架会使某些东西不再像POJO
#6
3
The whole point of a POJO is simplicity and you appear to be assuming its something more complicated than it appears.
POJO的重点在于简洁性,你似乎假设它比它看起来更复杂。
If a library supports a POJO, it implies an object of any class is acceptible. It doesn't mean the POJO cannot have annotations/interface or that they won't be used if they are there, but it is not a requirement.
如果库支持POJO,则意味着任何类的对象都是可加入的。这并不意味着POJO不能有注释/接口,或者如果它们在那里它们将不被使用,但它不是必需的。
IMHO The wiki-page is fairly clear. It doesn't say a POJO cannot have annotations/interfaces.
恕我直言wiki页面相当清楚。它并没有说POJO不能有注释/接口。
#7
0
A Plain Old Java Object (POJO) that contains all of the business logic for your extension.
包含扩展的所有业务逻辑的普通旧Java对象(POJO)。
Exp. Pojo which contains a single method
进出口。包含单个方法的Pojo
public class Extension {
public static void logInfo(String message) {
System.out.println(message);
}
}
#8
0
What does the term Plain Old Java Object (POJO) mean?
Plain Old Java Object(POJO)这个术语是什么意思?
POJO was coined by Martin Fowler, Rebecca Parsons and Josh Mackenzie when they were preparing for a talk at a conference in September 2000. Martin Fowler in Patterns of Enterprise Application Architecture explains how to implement a Domain Model pattern in Java. After enumerating some of disadvantages of using EJB Entity Beans:
POJO是由Martin Fowler,Rebecca Parsons和Josh Mackenzie在2000年9月的一次会议上准备演讲时创造的。企业应用程序架构模式中的Martin Fowler解释了如何在Java中实现域模型模式。在列举了使用EJB Entity Beans的一些缺点之后:
There's always a lot of heat generated when people talk about developing a Domain Model in J2EE. Many of the teaching materials and introductory J2EE books suggest that you use entity beans to develop a domain model, but there are some serious problems with this approach, at least with the current (2.0) specification.
当人们谈论在J2EE中开发域模型时,总会产生大量的热量。许多教材和介绍性J2EE书籍都建议您使用实体bean来开发域模型,但这种方法存在一些严重问题,至少在当前(2.0)规范中存在。
Entity beans are most useful when you use Container Managed Persistence (CMP)...
当您使用容器管理持久性(CMP)时,实体bean最有用...
Entity beans can't be re-entrant. That is, if you call out from one entity bean into another object, that other object (or any object it calls) can't call back into the first entity bean...
实体bean不能重入。也就是说,如果从一个实体bean调用另一个对象,那另一个对象(或它调用的任何对象)就不能回调到第一个实体bean ......
...If you have remote objects with fine-grained interfaces you get terrible performance...
...如果你有具有细粒度接口的远程对象,你会得到糟糕的表现......
To run with entity beans you need a container and a database connected. This will increase build times and also increase the time to do test runs since the tests have to execute against a database. Entity beans are also tricky to debug.
要使用实体bean运行,您需要连接容器和数据库。这将增加构建时间并且还增加了进行测试运行的时间,因为测试必须针对数据库执行。实体bean也很难调试。
As an alternative, he proposed to use Regular Java Objects for Domain Model implementation:
作为替代方案,他建议使用常规Java对象进行域模型实现:
The alternative is to use normal Java objects, although this often causes a surprised reaction—it's amazing how many people think that you can't run regular Java objects in an EJB container. I've come to the conclusion that people forget about regular Java objects because they haven't got a fancy name. That's why, while preparing for a talk in 2000, Rebecca Parsons, Josh Mackenzie, and I gave them one: POJOs (plain old Java objects). A POJO domain model is easy to put together, is quick to build, can run and test outside an EJB container, and is independent of EJB (maybe that's why EJB vendors don't encourage you to use them).
另一种方法是使用普通的Java对象,尽管这通常会引起惊讶的反应 - 很多人认为你不能在EJB容器中运行常规的Java对象,这是令人惊讶的。我得出结论,人们忘记了常规的Java对象,因为他们没有一个奇特的名字。这就是为什么,在准备2000年的演讲时,Rebecca Parsons,Josh Mackenzie和我给他们一个:POJO(普通的旧Java对象)。 POJO域模型易于组合,可快速构建,可在EJB容器外运行和测试,并且独立于EJB(这可能就是EJB供应商不鼓励您使用它们的原因)。
#1
55
Plain Old Java Object The name is used to emphasize that a given object is an ordinary Java Object, not a special object such as those defined by the EJB 2 framework.
Plain Old Java Object该名称用于强调给定对象是普通Java对象,而不是EJB 2框架定义的特殊对象。
class A {}
class B extends/implements C {}
class A {} class B扩展/实现C {}
Note: B is non POJO when C is kind of distributed framework class or ifc. e.g. javax.servlet.http.HttpServlet, javax.ejb.EntityBean or J2EE extn and not serializable/comparable. Since serializable/comparable are valid for POJO.
注意:当C是一种分布式框架类或ifc时,B是非POJO。例如javax.servlet.http.HttpServlet,javax.ejb.EntityBean或J2EE extn,不可序列化/可比较。由于可序列化/可比较对POJO有效。
Here A is simple object which is independent. B is a Special obj since B is extending/implementing C. So B object gets some more meaning from C and B is restrictive to follow the rules from C. and B is tightly coupled with distributed framework. Hence B object is not POJO from its definition.
这里A是一个独立的简单对象。 B是一个特殊的obj,因为B正在扩展/实现C.所以B对象从C获得更多的意义,B是限制性地遵循C的规则而B与分布式框架紧密耦合。因此B对象不是其定义中的POJO。
Code using class A object reference does not have to know anything about the type of it, and It can be used with many frameworks.
使用类A对象引用的代码不必知道它的类型,它可以与许多框架一起使用。
So a POJO should not have to 1) extend prespecified classes and 2) Implement prespecified interfaces.
所以POJO不应该1)扩展预先指定的类和2)实现预先指定的接口。
JavaBean is a example of POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.
JavaBean是可序列化的POJO示例,具有无参数构造函数,并允许使用遵循简单命名约定的getter和setter方法访问属性。
POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. It means it has the code for business logic but how this instance is created, Which service(EJB..) this object belongs to and what are its special characteristics( Stateful/Stateless) it has will be decided by the frameworks by using external xml file.
POJO完全专注于业务逻辑,并且不依赖于(企业)框架。这意味着它具有业务逻辑的代码但是如何创建该实例,该对象属于哪个服务(EJB ..)以及它具有的特殊特性(有状态/无状态)将由框架通过使用外部xml来决定文件。
Example 1: JAXB is the service to represent java object as XML; These java objects are simple and come up with default constructor getters and setters.
示例1:JAXB是将Java对象表示为XML的服务;这些java对象很简单,并提供了默认的构造函数getter和setter。
Example 2: Hibernate where simple java class will be used to represent a Table. columns will be its instances.
示例2:Hibernate,其中简单的java类将用于表示Table。列将是它的实例。
Example 3: REST services. In REST services we will have Service Layer and Dao Layer to perform some operations over DB. So Dao will have vendor specific queries and operations. Service Layer will be responsible to call Which DAO layer to perform DB operations. Create or Update API(methods) of DAO will be take POJOs as arguments, and update that POJOs and insert/update in to DB. These POJOs (Java class) will have only states(instance variables) of each column and its getters and setters.
示例3:REST服务。在REST服务中,我们将使用Service Layer和Dao Layer来对DB执行某些操作。所以Dao将有供应商特定的查询和操作。服务层将负责调用哪个DAO层来执行数据库操作。创建或更新DAO的API(方法)将POJO作为参数,并更新POJO并插入/更新到DB。这些POJO(Java类)将只包含每列的状态(实例变量)及其getter和setter。
In practice, some people find annotations elegant, while they see XML as verbose, ugly and hard to maintain, yet others find annotations pollute the POJO model. Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead or in addition to XML:
在实践中,有些人发现注释优雅,而他们认为XML冗长,丑陋且难以维护,而其他人则发现注释会污染POJO模型。因此,作为XML的替代方案,许多框架(例如Spring,EJB和JPA)允许使用注释来代替或者使用XML:
Advantages:
Decoupling the application code from the infrastructure frameworks is one of the many benefits of using POJOs. Using POJOs future proofs your application's business logic by decoupling it from volatile, constantly evolving infrastructure frameworks. Upgrading to a new version or switching to a different framework becomes easier and less risky. POJOs also make testing easier, which simplifies and accelerates development. Your business logic will be clearer and simpler because it won't be tangled with the infrastructure code
优点:将应用程序代码与基础架构框架分离是使用POJO的众多好处之一。使用POJO可以将应用程序的业务逻辑与不稳定,不断发展的基础架构框架分离,从而证明其应用程序的业务逻辑。升级到新版本或切换到不同的框架变得更容易,风险也更小。 POJO还使测试更容易,这简化并加速了开发。您的业务逻辑将更清晰,更简单,因为它不会与基础架构代码纠缠在一起
参考文献:wiki source2
#2
9
According to Martin Fowler, he and some others came up with it as a way to describe something which was a standard class as opposed to an EJB etc.
根据Martin Fowler的说法,他和其他一些人提出它是一种描述标准类而不是EJB等的方法。
#3
7
Usage of the term implies what it's supposed to tell you. If, for example, a dependency injection framework tells you that you can inject a POJO into any other POJO they want to say that you do not have to do anything special: there is no need to obey any contracts with your object, implement any interfaces or extend special classes. You can just use whatever you've already got.
该术语的使用意味着它应该告诉你的内容。例如,如果依赖注入框架告诉您可以将POJO注入任何其他POJO,他们想要说您不必执行任何特殊操作:不需要遵守与您的对象的任何契约,实现任何接口或延长特殊课程。你可以使用你已经拥有的任何东西。
UPDATE To give another example: while Hibernate can map any POJO (any object you created) to SQL tables, in Core Data (Objective C on the iPhone) your objects have to extend NSManagedObject in order for the system to be able to persist them to a database. In that sense, Core Data cannot work with any POJO (or rather POOCO=PlainOldObjectiveCObject) while Hibernate can. (I might not by 100% correct re Core Data since I just started picking it up. Any hints / corrections are welcome :-) ).
更新举另一个例子:虽然Hibernate可以将任何POJO(您创建的任何对象)映射到SQL表,但在Core Data(iPhone上的Objective C)中,您的对象必须扩展NSManagedObject,以便系统能够将它们持久化一个数据库。从这个意义上讲,Core Data不能与任何POJO(或者更确切地说是POOCO = PlainOldObjectiveCObject)一起工作,而Hibernate可以。 (我可能不会100%更正核心数据,因为我刚开始接受它。欢迎任何提示/更正:-))。
#4
6
Plain Old Java Object :)
普通的旧Java对象:)
Well, you make it sound like those are all terrible restrictions.
好吧,你听起来好像那些都是可怕的限制。
In the usual context where POJO is/are used, it's more like a benefit:
在使用POJO的通常情况下,它更像是一个好处:
It means that whatever library/API you're working with is perfectly willing to work with Java objects that haven't been doctored or manhandled in any way, i.e. you don't have to do anything special to get them to work.
这意味着您正在使用的任何库/ API都非常愿意使用未经过任何方式修改或处理过的Java对象,即您无需执行任何特殊操作即可使用它们。
For example, the XStream XML processor will (I think) happily serialize Java classes that don't implement the Serializable
interface. That's a plus! Many products that work with data objects used to force you to implement SomeProprietaryDataObject
or even extend an AbstractProprietaryDataObject
class. Many libraries will expect bean behavior, i.e. getters and setters.
例如,XStream XML处理器(我认为)将愉快地序列化未实现Serializable接口的Java类。这是一个加号!许多使用数据对象的产品用于强制您实现SomeProprietaryDataObject,甚至扩展AbstractProprietaryDataObject类。许多库都会期望bean的行为,即getter和setter。
Usually, whatever works with POJOs will also work with not-so-PO-JO's. So XStream will of course also serialize Serializable classes.
通常情况下,与POJO一起使用的任何东西都可以与不那么PO-JO一起使用。所以XStream当然也会序列化Serializable类。
#5
4
POJO is a Plain Old Java Object - as compared to something needing Enterprise Edition's (J2EE) stuff (beans etc...).
POJO是一个普通的旧Java对象 - 与需要企业版(J2EE)的东西(bean等)相比。
POJO is not really a hard-and-fast definition, and more of a hand-wavy way of describing "normal" non-enterprise Java Objects. Whether using an external library or framework makes an object POJO or not is kind of in the eye of the beholder, largely depending on WHAT library/framework, although I'd venture to guess that a framework would make something less of a POJO
POJO并不是一个非常快速的定义,更像是一种描述“普通”非企业Java对象的手工波形方式。无论是使用外部库还是框架使POJO成为对象,都会在旁观者看来,主要依赖于WHAT库/框架,尽管我冒昧地猜测框架会使某些东西不再像POJO
#6
3
The whole point of a POJO is simplicity and you appear to be assuming its something more complicated than it appears.
POJO的重点在于简洁性,你似乎假设它比它看起来更复杂。
If a library supports a POJO, it implies an object of any class is acceptible. It doesn't mean the POJO cannot have annotations/interface or that they won't be used if they are there, but it is not a requirement.
如果库支持POJO,则意味着任何类的对象都是可加入的。这并不意味着POJO不能有注释/接口,或者如果它们在那里它们将不被使用,但它不是必需的。
IMHO The wiki-page is fairly clear. It doesn't say a POJO cannot have annotations/interfaces.
恕我直言wiki页面相当清楚。它并没有说POJO不能有注释/接口。
#7
0
A Plain Old Java Object (POJO) that contains all of the business logic for your extension.
包含扩展的所有业务逻辑的普通旧Java对象(POJO)。
Exp. Pojo which contains a single method
进出口。包含单个方法的Pojo
public class Extension {
public static void logInfo(String message) {
System.out.println(message);
}
}
#8
0
What does the term Plain Old Java Object (POJO) mean?
Plain Old Java Object(POJO)这个术语是什么意思?
POJO was coined by Martin Fowler, Rebecca Parsons and Josh Mackenzie when they were preparing for a talk at a conference in September 2000. Martin Fowler in Patterns of Enterprise Application Architecture explains how to implement a Domain Model pattern in Java. After enumerating some of disadvantages of using EJB Entity Beans:
POJO是由Martin Fowler,Rebecca Parsons和Josh Mackenzie在2000年9月的一次会议上准备演讲时创造的。企业应用程序架构模式中的Martin Fowler解释了如何在Java中实现域模型模式。在列举了使用EJB Entity Beans的一些缺点之后:
There's always a lot of heat generated when people talk about developing a Domain Model in J2EE. Many of the teaching materials and introductory J2EE books suggest that you use entity beans to develop a domain model, but there are some serious problems with this approach, at least with the current (2.0) specification.
当人们谈论在J2EE中开发域模型时,总会产生大量的热量。许多教材和介绍性J2EE书籍都建议您使用实体bean来开发域模型,但这种方法存在一些严重问题,至少在当前(2.0)规范中存在。
Entity beans are most useful when you use Container Managed Persistence (CMP)...
当您使用容器管理持久性(CMP)时,实体bean最有用...
Entity beans can't be re-entrant. That is, if you call out from one entity bean into another object, that other object (or any object it calls) can't call back into the first entity bean...
实体bean不能重入。也就是说,如果从一个实体bean调用另一个对象,那另一个对象(或它调用的任何对象)就不能回调到第一个实体bean ......
...If you have remote objects with fine-grained interfaces you get terrible performance...
...如果你有具有细粒度接口的远程对象,你会得到糟糕的表现......
To run with entity beans you need a container and a database connected. This will increase build times and also increase the time to do test runs since the tests have to execute against a database. Entity beans are also tricky to debug.
要使用实体bean运行,您需要连接容器和数据库。这将增加构建时间并且还增加了进行测试运行的时间,因为测试必须针对数据库执行。实体bean也很难调试。
As an alternative, he proposed to use Regular Java Objects for Domain Model implementation:
作为替代方案,他建议使用常规Java对象进行域模型实现:
The alternative is to use normal Java objects, although this often causes a surprised reaction—it's amazing how many people think that you can't run regular Java objects in an EJB container. I've come to the conclusion that people forget about regular Java objects because they haven't got a fancy name. That's why, while preparing for a talk in 2000, Rebecca Parsons, Josh Mackenzie, and I gave them one: POJOs (plain old Java objects). A POJO domain model is easy to put together, is quick to build, can run and test outside an EJB container, and is independent of EJB (maybe that's why EJB vendors don't encourage you to use them).
另一种方法是使用普通的Java对象,尽管这通常会引起惊讶的反应 - 很多人认为你不能在EJB容器中运行常规的Java对象,这是令人惊讶的。我得出结论,人们忘记了常规的Java对象,因为他们没有一个奇特的名字。这就是为什么,在准备2000年的演讲时,Rebecca Parsons,Josh Mackenzie和我给他们一个:POJO(普通的旧Java对象)。 POJO域模型易于组合,可快速构建,可在EJB容器外运行和测试,并且独立于EJB(这可能就是EJB供应商不鼓励您使用它们的原因)。