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

后端 未结 3 1098
有刺的猬
有刺的猬 2021-02-05 23:11

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 differen

相关标签:
3条回答
  • 2021-02-05 23:41

    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.

    0 讨论(0)
  • 2021-02-05 23:48
    • Class defines an entity, while an object is the actualentity
    • Class is a conceptual modelthat defines allthecharacteristics and actions required of an object, whilean object is a real model
    • Class is a prototype of an object
    • All objects belonging to the same class have the samecharacteristics and actions
    0 讨论(0)
  • 2021-02-05 23:53

    In general a class is a construct which defines a set of properties and methods/functions while an Object is the actual instance of a class which is created at runtime.

    Sample class definition:

    public class Example{
    
    ...
    
    }
    

    The following will create an instance of the Example class as an Object at runtime;

    new Example();
    
    0 讨论(0)
提交回复
热议问题