Saturday, January 14, 2012

POJOs, Frameworks and Beans

POJO = Plain Old Java Object

While the acronym is catchy, there is nothing 'Plain' or 'Old' about POJOs.

A POJO is a regular Java class, which can contain complex methods and fields.
It can implement interfaces and extend classes.

So what makes it 'Plain'?

To understand that, we need to understand Frameworks and Beans .. and Containers.

A Framework is a formalized program (whether it is implemented as a jar or a standalone server or something else is not relevant) which allows you to accomplish certain tasks.

For example, Spring is an IoC/DI (Inversion of Control/Dependency Injection) framework.
Hibernate is a ORM (Object Relational Mapping) Framework.
HornetQ is a Messaging Framework.
J2EE itself is a Framework for writing Web Applications.
JSP is a templating Framework.
Struts is a MVC (Model-View-Controller) Framework.

All Frameworks are formal solutions to specific programming problems.
They define a formal API on how to use them - configurations, interfaces, classes, etc

The runtime avatar of a Framework is called a 'Container'.

So your web application can be said to be running inside the Tomcat container, your page templates are handled by the JSP container, your persistence is happening inside the Hibernate container, etc.

Often, to make use of a Container, you have to write Java classes which implement or extend certain specific, framework defined, interfaces or classes.

The Container is able to load these classes that you wrote and do certain things with them.
For example, Hibernate container can persist your Objects, JSP container can render your HTML files, JUnit container can run your unit tests, etc.

These classes that you write, that can be loaded by the container are called BEANS of that container.

Typically, a container will MANAGE the LIFECYCLE of its Beans - i.e. it will create the objects, call its methods, pass it around, and even destroy it when done. So a Bean is just an instance of the class, but one that is managed by the container.

Now coming back to POJOs:

Earlier frameworks typically required that Bean classes implement or extend certain specific Framework specified interfaces and classses.

This was a problem because when a newer version of the framework was released, the interfaces and classes needed to be updated.

To solve this problem, Frameworks now no longer require that Bean classes implement specific interfaces or extend specific classes.

They can be regular Java classes. The container loads them and uses Reflection to read them. This has turned out to be a big windfall for framework users, sparing them from having to update Bean classes with each new release.

These Bean classes, which are not required to implement or extend Framework- specific interfaces and classes, are called POJOs.

POJOs are, thus, regular Java classes - and nothing from the Framework "leaks" into them.

Let us see an example. We will compare a JUnit Bean, which is not a POJO - with a Hibernate Bean, which IS a POJO.

Here is the JUnit bean. You can see that it extends the JUnit specified class TestCase.
Hence it is not a POJO.
 import junit.framework.*; 

public class TestMath extends TestCase {

public void testAdd() {

int num1 = 3;

int num2 = 2;

int total = 5;

int sum = 0;

sum = Math.add(num1, num2);

assertEquals(sum, total);

}

}



On the other hand, here is a Hibernate Bean. You can see that it does not implement, nor extend any Hibernate specific interfaces or classes. In fact, there is nothing here to tell that it has anything to do with Hibernate. This is a POJO.

 package sample;

public class Artist {

String firstName;

String lastName;

long id;

public Artist () {super(); }

public Artist (String first, String last) {

firstName = first;

lastName = last;

}

public String getLastName() { return lastName; }

public String getFirstName() { return firstName; }

public void setFirstName( String name) { firstName = name; }

public void setLastName( String name) { lastName = name; }

public long getId() { return id; }

public void setId(long l) {id = l; }

public String toString() {return firstName + " " + lastName + id;}

}

No comments: