Decomposition — Example: Online Bookstore
The Starting Point
Goal: Build an online bookstore where customers can browse, search, buy books, and track orders. Administrators can manage inventory.
This is deliberately a "boring" system. It's well-understood, which lets us focus purely on the decomposition technique without domain complexity getting in the way.
Step 1: Top-Down — First Level
Ask: "What does an online bookstore need?"
Online Bookstore
├── Browsing & Search
├── Shopping Cart
├── Checkout & Payment
├── Order Management
└── Admin: Inventory
Five branches. Each is a major area of functionality. But none of these are concrete enough to build yet — "Browsing & Search" could mean a hundred different things.
Step 2: Second Level — Each Branch
Ask: "What does each of these need?"
Browsing & Search
Browsing & Search
├── List all books (paginated)
├── Search by keyword (title, author, or ISBN)
├── Filter by genre, price range, publication date
├── Sort by price / date / rating / relevance
└── View book details (description, reviews, availability)
Shopping Cart
Shopping Cart
├── Add item to cart
├── Remove item from cart
├── Update quantity
├── View cart summary (items, quantities, subtotal)
└── Cart persistence (survives page refresh, login/logout)
Checkout & Payment
Checkout & Payment
├── Enter shipping address
├── Validate shipping address
├── Select shipping method (standard, express)
├── Calculate order total (items + shipping + tax)
├── Enter payment information
├── Process payment
├── Create order record
└── Send confirmation email
Order Management
Order Management
├── View order history (list of past orders)
├── View single order details (items, shipping status, tracking)
├── Cancel order (if not yet shipped)
└── Request return (if within return window)
Admin: Inventory
Admin: Inventory
├── Add new book to catalog
├── Update book details (price, description, cover image)
├── Adjust stock levels (restock, corrections)
├── Mark book as discontinued
└── View low-stock alerts
Step 3: Check Each Leaf Against the Three Tests
For every leaf, ask:
- Can I write a contract for it? (Inputs, outputs, errors)
- Can I estimate the effort? (Small, medium, large)
- Can I identify its dependencies?
Let's test a few:
"Add item to cart" — Leaf Test
| Question | Answer |
|---|---|
| Contract? | ✅ IN: customer_id, book_id, quantity. OUT: updated cart. ERRORS: book not found, out of stock, invalid quantity. |
| Estimate? | ✅ Small — straightforward data operation |
| Dependencies? | ✅ Needs: Book Catalog (to verify the book exists and is in stock) |
Verdict: Concrete enough. This is a good leaf.
"Process payment" — Leaf Test
| Question | Answer |
|---|---|
| Contract? | ⚠️ Partially — but what about different payment methods? Retries? Partial payments? |
| Estimate? | ⚠️ "Medium" is a guess — there might be hidden complexity |
| Dependencies? | ✅ Needs: order total from "Calculate order total," payment info from "Enter payment" |
Verdict: Not quite decomposed enough. Let's break it down further.
Process Payment
├── Validate payment method (card not expired, sufficient funds estimate)
├── Submit payment to processor
├── Handle processor response (approved, declined, error)
├── Record payment result
└── Handle retry on transient failure
Now each sub-piece is concrete and estimable.
"Cart persistence" — Leaf Test
| Question | Answer |
|---|---|
| Contract? | ⚠️ This isn't an operation — it's a behavior. "Persist" isn't something a user does; it's something the system must maintain. |
| Estimate? | ⚠️ Depends on implementation approach (cookies? database? session?) |
| Dependencies? | ⚠️ Unclear |
Verdict: This is a requirement, not a leaf. It constrains how the cart works but doesn't decompose into an operation with a contract. Move it to a "requirements" list and let it inform the design of the other cart operations.
Step 4: Dependency Map
Now draw the dependency arrows:
Book Catalog ──────────────────── (foundation — depends on nothing)
│
▼
Shopping Cart ──── needs catalog for prices, stock checks
│
▼
Checkout ──── needs cart contents
│ │
│ ▼
│ Shipping ──── needs shipping address
│ │
│ ▼
│ Order Total ──── needs cart + shipping + tax rules
│ │
│ ▼
└─ Payment ──── needs order total
│
▼
Order Record ──── needs payment confirmation + cart + shipping
│
▼
Confirmation Email ──── needs order record
And separately:
Order Management ──── needs Order Record store (read-only)
Admin: Inventory ──── needs Book Catalog store (read-write)
What This Map Tells Us
Build order:
- Book Catalog (no dependencies)
- Shopping Cart (needs catalog)
- Shipping + Tax calculations
- Payment integration
- Order creation
- Email notifications
- Order management views
- Admin tools
Steps 3 and 4 can be built in parallel — they don't depend on each other.
Step 5: The Complete Tree
Online Bookstore
├── Book Catalog (foundation)
│ ├── Store book data (title, author, price, genre, description, cover)
│ ├── List books (paginated, sorted)
│ ├── Search books (keyword match against title/author/ISBN)
│ ├── Filter books (genre, price range, date range)
│ └── Get single book details
│
├── Shopping Cart
│ ├── Add item (book_id, quantity) → validate against catalog
│ ├── Remove item (line_item_id)
│ ├── Update quantity (line_item_id, new_quantity) → revalidate stock
│ └── Get cart summary (items, subtotal)
│
├── Checkout
│ ├── Enter shipping address
│ ├── Validate shipping address (real address? deliverable?)
│ ├── Select shipping method → calculate shipping cost
│ ├── Calculate order total (items + shipping + tax)
│ ├── Validate payment method
│ ├── Submit payment → handle response
│ ├── Record payment
│ ├── Create order record
│ └── Send confirmation email
│
├── Order Management (customer-facing)
│ ├── List my orders (paginated, newest first)
│ ├── View order details (items, status, tracking)
│ ├── Cancel order (if status = "processing")
│ └── Request return (if within 30-day window)
│
└── Admin: Inventory
├── Add book to catalog
├── Update book details
├── Adjust stock levels
├── Mark book as discontinued
└── View low-stock alerts (books with stock < threshold)
Total leaf count: 23 operations.
Each one can be contracted, estimated, and built. The original "build an online bookstore" has been transformed from a vague idea into 23 specific, concrete tasks with a clear build order.
The Estimation Table
| Leaf | Complexity | Dependencies | Priority | Notes |
|---|---|---|---|---|
| Store book data | Small | None | Critical | Foundation for everything |
| List books | Small | Book data | Critical | Core feature |
| Search books | Medium | Book data | Critical | Needs text matching logic |
| Filter books | Small | Book data | High | Simple query constraints |
| Get book details | Small | Book data | Critical | Used by cart and display |
| Add to cart | Small | Get book details | Critical | |
| Remove from cart | Small | None | Critical | |
| Update cart quantity | Small | Get book details (stock check) | High | |
| Get cart summary | Small | None | Critical | Checkout needs this |
| Enter shipping address | Small | None | Critical | |
| Validate shipping address | Medium | External validation service | High | Edge cases with international |
| Select shipping method | Small | Validated address | High | |
| Calculate order total | Small | Cart + shipping + tax rules | Critical | |
| Validate payment | Small | None | Critical | |
| Submit payment | Medium | External payment API + order total | Critical | Error handling is complex |
| Record payment | Small | Payment response | Critical | |
| Create order record | Small | Cart + payment + shipping | Critical | |
| Send confirmation email | Small | Order record | High | Can be async |
| List my orders | Small | Order records | High | |
| View order details | Small | Order records | High | |
| Cancel order | Small | Order record (status check) | Medium | |
| Request return | Medium | Order record + return policy rules | Low | Can ship v1 without it |
| Add book (admin) | Small | None | High | |
| Update book (admin) | Small | Book data | High | |
| Adjust stock (admin) | Small | Book data | High | |
| Discontinue book (admin) | Small | Book data | Medium | |
| Low-stock alerts (admin) | Small | Book data | Low | Nice to have for v1 |
Rough estimate: 9 medium tasks + 18 small tasks. This is a plannable project.
What This Example Teaches
- Start big, get specific — 5 branches became 23+ concrete leaves
- Test every leaf — if you can't contract it, it's not done decomposing
- Some "leaves" are actually requirements — cart persistence is a constraint, not an operation
- Dependencies give you build order — don't guess what to build first; the map tells you
- The boring system is a good training system — if you can fully decompose a bookstore, you can decompose anything