Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Projects and Practice

Project: Online Shop Model

Model an online shop at architecture level: components and interfaces, a checkout activity, a deployment view and OCL rules, then validate the wiring and rules in code.

The library project stayed at the level of classes and objects. This second project works one level higher, at the architecture of a system that must scale: which components exist, how orders flow through them, where they run and which rules they enforce. You use component, activity, deployment and class diagrams, plus OCL constraints. The goal is to see how the diagram types complement each other.

The brief

A small company sells goods online. Customers browse a catalog, fill a cart and pay by card. The shop checks stock, charges the card through an external payment provider, emails a receipt and tells a warehouse system to ship the order. In busy periods, traffic is high, so the web tier must run on at least two servers. The company wants to replace the payment provider later without touching the rest.

Step 1: components and interfaces

Start with replaceable parts. The last requirement ("replace the payment provider") already tells you where an interface is needed.

flowchart LR
    UI["Storefront UI"] -->|"ICatalog"| CAT["Catalog Service"]
    UI -->|"ICart"| CART["Cart Service"]
    UI -->|"IOrders"| ORD["Order Service"]
    ORD -->|"IStock"| INV["Inventory Service"]
    ORD -->|"IPayment"| PAYA["Payment Adapter"]
    ORD -->|"INotify"| MAIL["Notification Service"]
    ORD -->|"IShipping"| WH["Warehouse Gateway"]
    PAYA -.->|"HTTPS"| EXT["External Payment Provider"]
    WH -.->|"SOAP / REST"| WMS["Warehouse System"]

The adapter hides the provider: only PaymentAdapter knows the external API. Dashed lines leave our system.

Review questions: Does every required interface have a provider? Is there a cycle? (No.) Which component is the hub? (OrderService depends on four others: that is its job as orchestrator, but watch its size.)

Step 2: the checkout process as an activity diagram

The orchestration is a process with decisions and parallel work. Parallel work: after payment succeeds, the receipt email and the warehouse request do not depend on each other.

flowchart TD
    S(("start")) --> A["Validate cart"]
    A --> B["Reserve stock"]
    B --> D{"Stock OK?"}
    D -->|"no"| X["Tell customer: sold out"] --> E1(("end"))
    D -->|"yes"| P["Charge card"]
    P --> Q{"Approved?"}
    Q -->|"no"| R["Release stock"] --> E2(("end"))
    Q -->|"yes"| F{{"fork"}}
    F --> M["Send receipt"]
    F --> W["Request shipping"]
    M --> J{{"join"}}
    W --> J
    J --> E3(("end"))

The fork and join bars (drawn as hexagons in Mermaid) show the parallelism. The decision diamonds have a guard on every branch. The "Release stock" step is easy to forget: it is the compensating action that keeps the inventory correct when payment fails. Activity diagrams make such missing branches visible.

Step 3: domain rules as OCL constraints

Some rules do not fit on any arrow. Write them as invariants and conditions on operations, using the notation from OCL Constraints.

TEXT
context Order
inv hasLines:      self.lines->notEmpty()
inv totalMatches:  self.total = self.lines->collect(l | l.unitPrice * l.qty)->sum()
inv positiveQty:   self.lines->forAll(l | l.qty > 0)

context InventoryService::reserve(sku : String, qty : Integer)
pre  enough:   self.available(sku) >= qty
post reduced:  self.available(sku) = self.available(sku)@pre - qty

Step 4: deployment view

The web tier must run on at least two servers, so the deployment diagram needs a load balancer and two app nodes, plus a database.

flowchart TB
    Browser["Customer browser"] -->|"HTTPS"| LB["Load balancer"]
    subgraph Cluster["Application cluster"]
        N1["App node 1<br/>shop.jar"]
        N2["App node 2<br/>shop.jar"]
    end
    LB --> N1
    LB --> N2
    N1 -->|"TCP 5432"| DB[("PostgreSQL primary")]
    N2 -->|"TCP 5432"| DB
    DB -.->|"replication"| DBR[("PostgreSQL replica")]
    N1 -.->|"HTTPS"| PAY["Payment provider"]
    N2 -.->|"HTTPS"| PAY

Checks: the load balancer and the primary database are single points of failure. Mention that in the diagram notes and decide whether the risk is acceptable. The components of step 1 are all packaged in one artifact (shop.jar) for now: a manifestation note records this, and a future split into services changes only the deployment diagram.

Step 5: validate the architecture in code

The component model from step 1 is data. Two checks catch common errors: a required interface with no provider, and a cycle.

Python Check interfaces and cycles of the shop components

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 6: run the checkout flow with compensation

Finally, execute the activity diagram. The function returns the list of steps it took, so you can compare it with the diagram path for each scenario.

Python Execute the checkout activity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three scenarios give three paths through the activity diagram: exactly the path-coverage idea from Sequence Diagrams to Tests.

Extensions

  • Draw the sequence diagram for "Charge card" with a timeout and a retry (loop, alt).
  • Add a state machine for Order (Created, Paid, Shipped, Delivered, Cancelled, Refunded) and compare it with the activity diagram.
  • Split shop.jar into two services and update the component and deployment diagrams.
  • Add a <<profile>> with stereotypes <<service>> and <<gateway>> to the components.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the model have a Payment Adapter component?

    Only the adapter knows the provider's API, so a replacement affects one component.
  2. 2. What does the 'Release stock' step in the activity diagram represent?

    Compensation undoes an earlier step when a later one fails.
  3. 3. Which diagram shows that receipt and shipping requests happen in parallel?

    Fork and join bars show parallel flows.
  4. 4. Which deployment risk does the diagram reveal?

    Only the app tier is redundant; the other two nodes have no spare.

Summary

  • Architecture modeling combines component, activity, deployment and class views with OCL rules.
  • Interfaces at the volatile edges (payment, warehouse) let you replace parts later.
  • Activity diagrams reveal forgotten branches such as compensation.
  • Deployment diagrams show redundancy and single points of failure.
  • Component models are data: check unsatisfied interfaces and cycles automatically.

Next: Project: ATM State and Sequence Model