Value Object
Value Object is design pattern describing
"A small simple object, like money or a date range, whose equality isn't based on identity." [PoEAA]
Since Java (contrary for example to C#) has no built-in support for values other than primitive types this pattern is recommended to use when one wants to implement value semantics for some data type.
The most popular way
The most popular way of implementing ValueObject is by
making object immutable using private final fields and no setters and other methods modifying it's state. For great example of this kind of implementation please refer to the source code of java.lang.String class.
Shorter way
In many cases this kind of implementation can be replaced with shorter one. Instead of making fields private and using getters to obtain their value we can just mark them as public final.
package koppernickus;
public final class SimpleValue {
public final int propertyA;
public final String propertyB;
public SimpleValue(int propertyA, String propertyB) {
this.propertyA = propertyA;
this.propertyB = propertyB;
}
}
This way we:
- write less code,
- make accessing values slightly shorter, for example:
int a = value.propertyA;
instead of
int a = value.getPropertyA();
Controversies
There is a lot of controversy about that method among my colleagues. However I think that there are some arguments for that approach, for example Uncle Bob (Robert C. Martin) seems to support this way of implementing, at least for Data Transfer Object pattern (which is very often implemented using Value Object). Uncle Bob in [CleanCode] writes:
The quintessential form of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO. DTOs are very useful structures, especially when communicating with databases or parsing messages from sockets, and so on. They often become the first in a series of translation stages that convert raw data
in a database into objects in the application code. Somewhat more common is the “bean” form shown in Listing 6-7. Beans have private variables manipulated by getters and setters. The quasi-encapsulation of beans seems to make some OO purists feel better but usually provides no other benefit.
When not to use?
Don't use this way if at least one of the fields is mutable (for example is an array or "standard" collection). Then to ensure immutability of the object you will have to encapsulate that field with "copying getter" (
Defensive copying). Then having some fields directly accessible and some accessible via getters is not consistent and may lead to unnecessary confusion.
References
[PoEAA] Fowler, M. 2002
Patterns of Enterprise Application Architecture. Addison-Wesley Longman Publishing Co., Inc.
[Refactoring] Fowler, M. 1999
Refactoring: Improving the Design of Existing Code. Addison-Wesley Longman Publishing Co., Inc.
[CleanCode] Martin, R. 2008
Clean Code: a Handbook of Agile Software Craftsmanship. Prentice Hall PTR.
[javapractices]
http://www.javapractices.com/topic/TopicAction.do?Id=29