Plain Old Java Object(POJO)这个术语到底意味着什么? [英] What does the term Plain Old Java Object (POJO) exactly mean?

查看:782
本文介绍了Plain Old Java Object(POJO)这个术语到底意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

术语 Plain Old Java Object(POJO)是什么意思?我找不到足够的解释。

What does the term Plain Old Java Object (POJO) mean? I couldn't find anything explanatory enough.

POJO's维基百科页面说POJO是一个普通的Java对象,而不是一个特殊的对象。现在,在Java中做什么或者什么不做和对象特别?

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实现接口,如 Serializable Comparable 或类似Applet或任何其他用户的类写的类/接口?

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?

此外,上述政策(没有延期,没有实施)是否意味着我们不允许使用任何外部图书馆?

Also, does the above policy (no extending, no implementing) means that we are not allowed to use any external libraries?

POJO究竟在哪里使用?

Where exactly are POJOs used?

编辑:更具体一点,我是否允许扩展/实现类/接口它是Java或任何外部库的一部分吗?

To be more specific, am I allowed to extend/implement classes/interfaces that are part of the Java or any external libraries?

推荐答案

普通旧Java对象名称是用于强调给定对象是普通Java对象,而不是EJB 2框架定义的特殊对象。

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.

class A {}

类B扩展/实现C {}

class A {}
class B extends/implements C {}

注意:当C是一种分布式框架类或ifc时,B是非POJO。
,例如javax.servlet.http.HttpServlet,javax.ejb.EntityBean或J2EE extn
且不可序列化/可比较。由于可序列化/可比较对POJO有效。

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.

这里A是一个独立的简单对象。
B是一个特殊的obj,因为B正在扩展/实现C.所以B对象从C中得到更多的含义,B是限制性地遵循C中的规则而B是紧密耦合分布式框架。因此B对象不是定义中的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的代码对象引用不需要知道它的类型,它可以用于许多框架。

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.

所以POJO不应该1)扩展预先指定的类和2)实现预先指定的接口。

So a POJO should not have to 1) extend prespecified classes and 2) Implement prespecified interfaces.

JavaBean是POJO的一个示例,它是可序列化的,具有无参数构造函数,并允许使用遵循简单命名约定的getter和setter方法访问属性。

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.

POJO完全专注于业务逻辑,并且不依赖于(企业)框架。
这意味着它拥有业务逻辑的代码但是如何创建这个实例,这个对象属于哪个服务(EJB ..)以及它具有的特殊特性(Stateful / Stateless)将由框架决定通过使用外部xml文件。

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.

示例1:JAXB是将Java对象表示为XML的服务;这些java对象很简单,并提供了默认的构造函数getter和setter。

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.

示例2:Hibernate,其中简单的java类将用于表示Table。列将是它的实例。

Example 2: Hibernate where simple java class will be used to represent a Table. columns will be its instances.

示例3:REST服务。在REST服务中,我们将使用Service Layer和Dao Layer来对DB执行某些操作。所以Dao将有供应商特定的查询和操作。服务层将负责调用哪个DAO层来执行数据库操作。创建或更新DAO的API(方法)将POJO作为参数,并更新POJO并插入/更新到DB。这些POJO(Java类)将只包含每列的状态(实例变量)及其getter和setter。

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.

在实践中,有些人发现注释优雅,而他们看到XML虽然冗长,丑陋且难以维护,但其他人发现注释污染了POJO模型。
因此,作为XML的替代方案,许多框架(例如Spring,EJB和JPA)允许使用注释来替代或补充XML:

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:

< B>优点:

将应用程序代码与基础架构框架分离是使用POJO的众多好处之一。使用POJO可以将应用程序的业务逻辑与不稳定,不断发展的基础架构框架分离,从而证明其应用程序的业务逻辑。升级到新版本或切换到不同的框架变得更容易,风险也更小。 POJO还使测试更容易,这简化并加速了开发。您的业​​务逻辑将更清晰,更简单,因为它不会与基础架构代码纠缠在一起

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

参考文献: wiki source2

这篇关于Plain Old Java Object(POJO)这个术语到底意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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