DDD Aggregate Example
This program models DDD aggregate.
Problem Statement
Write a Java program where Order is an aggregate root containing Product.
Source Code
| 1 | class Product { |
| 2 | String name; double price; |
| 3 | Product(String n,double p){ name=n; price=p; } |
| 4 | } |
| 5 | class Order { |
| 6 | Product product; int qty; |
| 7 | Order(Product p,int q){ product=p; qty=q; } |
| 8 | void show(){ System.out.println("Order: "+product.name+", Qty: "+qty+", Total: "+(product.price*qty)); } |
| 9 | } |
| 10 | public class DDDAggregate { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq DDD Aggregate"); |
| 13 | Product p=new Product("Laptop",50000); |
| 14 | Order o=new Order(p,2); |
| 15 | o.show(); |
| 16 | } |
| 17 | } |
Program Output
Eduinq DDD Aggregate Order: Laptop, Qty: 2, Total: 100000.0
Explanation
Aggregate root controls access to related entities. - Order acts as aggregate root, containing Product. - This ensures consistency and encapsulation in domain models. Conclusion: Shows DDD aggregate example.