What does the term Plain Old Java Object (POJO) ex

2019-01-21 03:26发布

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

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?

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?

Where exactly are POJOs used?

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

8条回答
祖国的老花朵
2楼-- · 2019-01-21 03:39

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.

查看更多
Fickle 薄情
3楼-- · 2019-01-21 03:46

What does the term Plain Old Java Object (POJO) mean?

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:

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.

Entity beans are most useful when you use Container Managed Persistence (CMP)...

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...

...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.

As an alternative, he proposed to use Regular Java Objects for Domain Model implementation:

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).

查看更多
登录 后发表回答