Plain Old Java Object or POJO is just an ordinary Java object, The term was originally coined by Martin Fowler, Rebecca Parsons, and Josh Mackenzie in September 2000.
According to Martin Fowler
Generally, a POJO is not bound to any restriction and any Java object can be called a POJO but there are some directions. A well-defined POJO should follow below directions.
And according to Java Language Specification, a POJO should not have to
However, developers and frameworks describe a POJO still requires the use prespecified annotations to implement features like persistence, declarative transaction management etc. So the idea is that if the object was a POJO before any annotations were added would return to POJO status if the annotations are removed then it can still be considered a POJO.
According to Martin Fowler
The term was coined while Rebecca Parsons, Josh MacKenzie and I were preparing for a talk at a conference in September 2000. In the talk we were pointing out the many benefits of encoding business logic into regular java objects rather than using Entity Beans. 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.
Generally, a POJO is not bound to any restriction and any Java object can be called a POJO but there are some directions. A well-defined POJO should follow below directions.
- Each variable in a POJO should be declared as private.
- Default constructor should be overridden with public accessibility.
- Each variable should have its Setter-Getter method with public accessibility.
- Generally POJO should override equals(), hashCode() and toString() methods of Object (but it's not mandatory).
- Overriding compare() method of Comparable interface used for sorting (Preferable but not mandatory).
private long id;
private String value = "";
private Collection<Term> children = Collections.emptyList();
public Term() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Collection<Term> getChildren() {
return children;
}
public void setChildren(Collection<Term> children) {
this.children = children;
}
Overriding these methods will help us while using our POJO with different Java collection classes or writing it directly to console or inspecting it in debugger.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
result = prime * result + ((children == null) ? 0 : children.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Term other = (Term) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
return true;
}
@Override
public String toString() {
return "Term [id=" + id + ", value=" + value + ", children=" + children + "]";
}
@Override
public int compareTo(Term term) {
return (int) (this.id - term.id);
}
And according to Java Language Specification, a POJO should not have to
- Extend prespecified classes, as in
- Implement prespecified interfaces, as in
- Contain prespecified annotations, as in
public class Foo extends javax.servlet.http.HttpServlet {
}
public class Bar implements javax.ejb.EntityBean {
}
@javax.persistence.Entity
public class Baz {
}
However, developers and frameworks describe a POJO still requires the use prespecified annotations to implement features like persistence, declarative transaction management etc. So the idea is that if the object was a POJO before any annotations were added would return to POJO status if the annotations are removed then it can still be considered a POJO.
A JavaBean is a special kind 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.
javascript code examples
ReplyDelete