Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class, Object, Entity: What's the difference?

I also see other terms as well: Entity Object, Value Object, etc. Are there other terms out there that I should know, and what do these terms refer to?

Can the differences between them, if any, be identified by reading code?

like image 651
Jan Julian Avatar asked Jan 04 '13 17:01

Jan Julian


1 Answers

A class is a template for creating objects. Not all OO languages use classes (see Self, Javascript). Typically classes are implemented as objects.

An object is a bundle of data that is packaged with functions that act on that data (called methods). Calling a class's constructor allocates memory for the object and initializes its member variables.

An entity is an object that represents something that has an identity that the system is interested in tracking. Typical examples are Customers and Accounts.

A value object is a value, it doesn't have an identity, and two instances with the same value are considered to be identical. Typical examples are monetary amounts, locations, payment types.

A data transfer object is used for passing a bunch of data around. Typically they're used in distributed systems to send data as a bundle in order to avoid repeated network calls. Data transfer objects have no identity (or there is no expectation they should have any), they are just containers for data.

Generally you can tell the difference between entities and value objects because entities have a recognizable identity, and the system is concerned with creating them, storing them, and changing them. In cases where objects map to some database, entities have primary keys that are either some kind of composite natural key or an artificial key, while value objects are compared by value.

like image 187
Nathan Hughes Avatar answered Oct 19 '22 12:10

Nathan Hughes