Package cohesion
Elements in a package should belong together from a change and responsibility standpoint.
Many systems improve individual classes while leaving the package structure muddled. Package-level design matters because it shapes ownership, deployment, dependency direction, and how people mentally navigate the codebase. Class quality alone cannot rescue weak package boundaries.
In an order platform, package boundaries often reveal whether the system is organized around domain concepts, technical layers, or a mix of both. Poor package cohesion leads to change scattering. Poor package coupling leads to ripple effects across modules and teams.
Package principles scale cohesion and coupling up one level. A class can be clean while the package still mixes domain, infrastructure, API, and persistence details in ways that make the system hard to evolve.
In Java, package structure is part of the design language. It signals ownership and dependency direction. OOAD becomes more durable when package boundaries reflect domain responsibilities and architectural intent.
The package test is similar to the class test: what kinds of changes should need to happen here together, and which changes should not?
Elements in a package should belong together from a change and responsibility standpoint.
Dependencies between packages should be limited, stable, and easy to reason about.
Package design often determines whether modules are teachable, testable, and refactorable.
package org.javaomnibus.ecommerce.ordering;
public final class PlaceOrderUseCase {
private final PaymentPort paymentPort;
private final InventoryPort inventoryPort;
public PlaceOrderUseCase(PaymentPort paymentPort, InventoryPort inventoryPort) {
this.paymentPort = paymentPort;
this.inventoryPort = inventoryPort;
}
public void handle(Order order) {
inventoryPort.reserve(order);
paymentPort.authorize(order);
}
}
interface PaymentPort { void authorize(Order order); }
interface InventoryPort { void reserve(Order order); }
record Order(String orderId) {}
Over-optimizing package structure too early can create ceremony and churn. Good boundaries matter, but they should reflect real ownership and change patterns, not just an idealized diagram.