Skip to main content

Overview

The Order class is the root aggregate in the FoodTech Kitchen domain model. It represents a customer order from a specific table and contains all products requested. Orders are immutable once created and serve as the starting point for task distribution to kitchen stations.
Orders follow the Aggregate Root pattern in Domain-Driven Design, ensuring all product modifications go through the Order entity.

Class Definition

package com.foodtech.kitchen.domain.model;

import java.util.ArrayList;
import java.util.List;

public class Order {
    private final Long id;
    private final String tableNumber;
    private final List<Product> products;

    public Order(String tableNumber, List<Product> products) {
        validate(tableNumber, products);
        this.id = null;
        this.tableNumber = tableNumber;
        this.products = new ArrayList<>(products);
    }
}

Fields

id
Long
Unique identifier for the order. Set to null on creation and assigned by the persistence layer.
tableNumber
String
required
The table number where the order was placed. Cannot be null or empty.
products
List<Product>
required
List of products included in this order. Must contain at least one product. The list is defensively copied to ensure immutability.

Constructors

Public Constructor

public Order(String tableNumber, List<Product> products)
Creates a new Order without an ID (for new orders before persistence). Parameters:
  • tableNumber - The table number (required, cannot be null or empty)
  • products - List of products to include (required, cannot be null or empty)
Throws:
  • IllegalArgumentException if tableNumber is null/empty
  • IllegalArgumentException if products list is null/empty
The products list is defensively copied. Changes to the original list after construction will not affect the Order.

Reconstruction Factory Method

public static Order reconstruct(Long id, String tableNumber, List<Product> products)
Reconstructs an existing Order from persistence with its ID. Parameters:
  • id - The order ID (required, cannot be null)
  • tableNumber - The table number
  • products - List of products
Throws:
  • IllegalArgumentException if ID is null
  • IllegalArgumentException if validation fails
Use this factory method when loading orders from the database to maintain domain model integrity.

Validation Rules

The Order class enforces the following business rules:
RuleValidationException
Table NumberCannot be null or empty (after trim)IllegalArgumentException
Products ListCannot be null or emptyIllegalArgumentException
ID on ReconstructionMust not be null when reconstructingIllegalArgumentException

Validation Implementation

private void validate(String tableNumber, List<Product> products) {
    if (tableNumber == null || tableNumber.trim().isEmpty()) {
        throw new IllegalArgumentException("Table number cannot be null or empty");
    }
    if (products == null || products.isEmpty()) {
        throw new IllegalArgumentException("Products list cannot be null or empty");
    }
}

private static void validateId(Long id){
    if (id == null) {
        throw new IllegalArgumentException("ID cannot be null when reconstructing Order");
    }
}

Public Methods

getId()

public Long getId()
Returns the order ID, or null if the order hasn’t been persisted yet.

getTableNumber()

public String getTableNumber()
Returns the table number associated with this order.

getProducts()

public List<Product> getProducts()
Returns a defensive copy of the products list. Modifications to the returned list will not affect the Order’s internal state.
All getters return either immutable values or defensive copies, ensuring the Order remains immutable after creation.

Usage Example

// Creating a new order
List<Product> products = Arrays.asList(
    new Product("Caesar Salad", ProductType.COLD_DISH),
    new Product("Grilled Steak", ProductType.HOT_DISH),
    new Product("Mojito", ProductType.DRINK)
);

Order order = new Order("T-15", products);

// Accessing order information
String table = order.getTableNumber(); // "T-15"
List<Product> orderProducts = order.getProducts(); // Returns defensive copy

// Reconstructing an order from database
Order existingOrder = Order.reconstruct(123L, "T-15", products);
Long orderId = existingOrder.getId(); // 123L

Design Patterns

Immutability Pattern

All fields are final and the products list is defensively copied, making Order instances immutable after creation.

Factory Method Pattern

The reconstruct() static factory method provides a clear way to create orders with IDs from persistence.

Defensive Copying

Both constructor and getter create copies of the products list to prevent external modifications.
  • Product - Products contained in the order
  • Task - Tasks generated from orders for each station

Build docs developers (and LLMs) love