Entity

In Domain-Driven Design (DDD), an entity is a core concept that represents a domain object with a unique identity. An entity is an object that is distinguished from other objects based on its unique identity, rather than its attributes or values.

Entities are usually the most important objects in a domain model, and they often have complex business logic and behavior associated with them. They can also have relationships with other entities, value objects, or domain services.

An entity has the following characteristics:

  1. Identity: An entity has a unique identity that distinguishes it from other entities in the domain model. The identity is typically represented by an ID or a key, such as a customer ID or a product SKU.
  2. Mutability: An entity’s attributes can change over time while maintaining its identity. For example, a customer’s name or address may change, but the customer ID remains the same.
  3. Behavior: An entity has behavior associated with it, often complex business logic and rules. This behavior is usually encapsulated within the entity itself.
  4. Relationship: An entity can have relationships with other entities, value objects, or domain services. For example, a customer may have an order history or a shopping cart.

Entities are an essential part of the domain model and should be designed to accurately represent the domain and its business rules. By modeling entities correctly, developers can create a more flexible and maintainable software solution that meets the needs of the domain.

An example

Consider an e-commerce platform where customers can place orders for products. In this domain model, the Order is an entity. Each order has a unique and immutable identity, such as an order number, which distinguishes it from other orders in the system.

The Order entity can have several attributes, such as customer information, payment details, and shipping information. It can also have relationships with other entities, such as Product and Customer entities.

The behavior of the Order entity includes creating and updating orders, managing payment processing, and tracking order status.

Here is an example of what an Order entity might look like in code:

public class Order {
    private OrderId orderId;
    private Customer customer;
    private List<Product> products;
    private Date orderDate;
    private PaymentDetails paymentDetails;
    private ShippingDetails shippingDetails;
    
    public Order(OrderId orderId, Customer customer) {
        this.orderId = orderId;
        this.customer = customer;
        this.products = Lists.newArrayList();
        this.orderDate = LocalDate.now();
    }
        
    public void addProduct(Product product) {
        products.add(product);
    }
    
    public void removeProduct(Product product) {
        products.remove(product);
    }
    
    public void processPayment() {
        // Process payment logic here...
    }
    
    public void shipOrder() {
        // Shipping logic here...
    }
    
    // Other behavior methods here...
}

In this example, the Order entity has a unique ID (orderId) that identifies it within the system, along with other attributes like customer, products, orderDate, paymentDetails, and shippingDetails. The entity also has behavior methods, such as addProduct, removeProduct, processPayment, and shipOrder, which encapsulate the business logic associated with the order entity.