Overview
TheOrder 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
Fields
Unique identifier for the order. Set to
null on creation and assigned by the persistence layer.The table number where the order was placed. Cannot be null or empty.
List of products included in this order. Must contain at least one product. The list is defensively copied to ensure immutability.
Constructors
Public Constructor
tableNumber- The table number (required, cannot be null or empty)products- List of products to include (required, cannot be null or empty)
IllegalArgumentExceptionif tableNumber is null/emptyIllegalArgumentExceptionif products list is null/empty
Reconstruction Factory Method
id- The order ID (required, cannot be null)tableNumber- The table numberproducts- List of products
IllegalArgumentExceptionif ID is nullIllegalArgumentExceptionif validation fails
Validation Rules
The Order class enforces the following business rules:| Rule | Validation | Exception |
|---|---|---|
| Table Number | Cannot be null or empty (after trim) | IllegalArgumentException |
| Products List | Cannot be null or empty | IllegalArgumentException |
| ID on Reconstruction | Must not be null when reconstructing | IllegalArgumentException |
Validation Implementation
Public Methods
getId()
null if the order hasn’t been persisted yet.
getTableNumber()
getProducts()
All getters return either immutable values or defensive copies, ensuring the Order remains immutable after creation.
Usage Example
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.