Module contracts
What you will learn
A modular monolith runs as one deployable application while keeping feature ownership and contracts explicit. Start with a use case and expose the smallest stable contract that another feature actually needs.
- Distinguish deployment boundaries from logical modules.
- Define an immutable feature contract and translate another model at its boundary.
- Compare layered, hexagonal, clean and vertical-slice designs.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 models; the companion JPMS project has separate build instructions.
A narrow inventory contract
public class Main { interface Stock { int available(String sku); } record Quote(String sku, boolean available) {} static Quote quote(Stock stock, String sku, int quantity) { if (quantity <= 0) throw new IllegalArgumentException("quantity"); return new Quote(sku, stock.available(sku) >= quantity); } public static void main(String[] args) { Stock stock = sku -> sku.equals("PEN") ? 5 : 0; System.out.println(quote(stock, "PEN", 2)); System.out.println(quote(stock, "BOX", 1)); }}Expected output:
Quote[sku=PEN, available=true]Quote[sku=BOX, available=false]The order feature knows only the availability contract. A quote does not reserve stock; the actual reservation must check and update atomically.
Organize by responsibility and feature
A layered design groups presentation, application logic and infrastructure by role. Package-by-feature puts related order behavior together, possibly with layers inside each feature. Vertical slices organize around a use case such as placing an order. Clean architecture emphasizes policy independence; ports and adapters makes external interactions replaceable. These ideas overlap but are not identical. A monolith can use any of them; a folder alone enforces no dependency rule.
Contracts and ownership
An order feature owns its order state. Inventory owns available quantities. Share IDs and purpose-specific immutable request/result types, rather than mutable entities or repository internals. Validate input at the receiving boundary and define failure meaning. An anti-corruption adapter translates a foreign model into local terms instead of importing its assumptions throughout the domain.
Dependency map
Text diagram: CLI → Order application → Inventory reservation contract. Inventory adapter implements the contract; the composition root wires the two. Arrows indicate source dependencies, not timing of method calls. A single deployment avoids a network hop, but does not guarantee transaction atomicity, thread safety or independent scaling.
Translate a legacy inventory model
public class Main { record Legacy(String code, int physical, int held) {} record Availability(String sku, int free) {} static Availability translate(Legacy item) { if (item.held() < 0 || item.physical() < item.held()) throw new IllegalArgumentException("inconsistent inventory"); return new Availability(item.code(), item.physical() - item.held()); } public static void main(String[] args) { System.out.println(translate(new Legacy("PEN", 8, 3))); System.out.println(translate(new Legacy("BOX", 2, 2))); }}Expected output:
Availability[sku=PEN, free=5]Availability[sku=BOX, free=0]The adapter translates physical and held stock into the local availability meaning. Reject inconsistent upstream data rather than silently inventing a valid result.
Return a snapshot instead of exposing state
public class Main { record Report(java.util.List<String> ids) { Report { ids = java.util.List.copyOf(ids); } } public static void main(String[] args) { var ids = new java.util.ArrayList<>(java.util.List.of("O1")); var report = new Report(ids); ids.add("O2"); System.out.println(report.ids()); }}Expected output:
[O1]Copying the list protects the report from later structural edits. Strings are immutable; mutable elements would require their own copying policy.
Common mistakes
Do not share a mutable entity across features or treat a package named api as automatic enforcement. A single process does not remove the need to define failure and consistency behavior.
Practice
Implement a shipping quote port that returns 500 cents for zone A and rejects unknown zones. Call the port for A and B, printing the quote and the rejection.
Expected output:
500rejectedCompare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Module contracts — knowledge check
Modular architecture and contract reasoning.
What is a modular monolith?
Which object should cross a feature boundary?
What does an anti-corruption adapter do?
What is the purpose of a port?
Does an availability quote reserve stock?