What is Value Objects

A Value Object is something that hold values but not the identity. Value Object is an immutable type that is distinguishable only by the state of its attributes. i.e. Two Value Objects will be considered same if they share all of their attributes. Value Objects has all the properties of an immutable object like String in Java. Like, Value Objects cannot be changed once they are created. The Value Object can define helper methods that assist with such operations like equals and hashcode etc.

Value Objects are not reusable. You can not use one object for multiple purposes because each of them is an individual for itself. For each client in a bank you have to create his own account. The performance part means that if you have a lot of these “individuals” you are going to end up with a lot of them which first of all can fill up your memory and secondly you will lose a lot of time for the creation of each of them

Example:  Lets assume we have below relationship between classes Employee and Designation

So

public class Employee {
private Designation designation;

}

public class Designation {
private String post;

}

Now lets say there is an Employee e who’s current designation is intern in his/her company. Say, he/she got promoted to trainee. So the same employee e’s designation will be changed to “Trainee” from “Intern”. Every time you get promotion, your designation changes from A -> B -> C -> D … and so on. Modifying one is conceptually the same as discarding the old one and creating a new one. Two Employees sharing same designation are not same but their designations are same.

So in this case, Designation becomes a good candidate for a Value Object.

Another example-

Lets say we have 4 security cameras installed in four sides(Locations) of our building and their job is to notify security team if they see any intruders entering illegally in the building. So in this scenario you can consider that Intruder object as Value Object. Because the security cameras are not interested in the identity of the intruder. For them every intruder is new and they have to notify the security team.

Hope this has cleared your doubts about Value Objects


One thought on “What is Value Objects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s