System Design
Object-Oriented Design
Logistics System

Design Example: Logistics System

A Logistics System (like FedEx or DHL) manages the lifecycle of shipments, tracks their physical location, and optimizes delivery routes.


Step 1: Requirements Gathering

Core Use Cases:

  • Shipment Creation: Users can create a shipment with destination and service type (Ground, Air, Express).
  • Tracking: Real-time status updates (Picked up, In Transit, Out for Delivery, Delivered).
  • Vehicles & Drivers: Assign shipments to specific trucks or planes.
  • Route Optimization: Calculate the most efficient path between hubs.
  • Billing: Charge based on weight, distance, and service speed.

Shipment States:

  • Status: BOOKED, PICKED_UP, IN_TRANSIT, HELD_AT_HUB, OUT_FOR_DELIVERY, DELIVERED, RETURNED.

Step 2: Identify Core Objects

  • Shipment: The primary entity containing items, weight, and route.
  • Account: User (Sender/Receiver) and Employee (Driver/Dispatcher).
  • Vehicle: Truck, Plane, or Bike with capacity limits.
  • Hub: Physical locations where shipments are sorted.
  • ServiceType: Defines speed and cost (Ground vs Express).
  • TrackingLog: Historical record of status changes.

Step 3: Design Class Diagram


Step 4: Implementation in TypeScript

enum ShipmentStatus { BOOKED, IN_TRANSIT, DELIVERED }
 
class Shipment {
  private status: ShipmentStatus = ShipmentStatus.BOOKED;
  private logs: TrackingLog[] = [];
 
  constructor(
    public id: string,
    public weight: number,
    public destination: string
  ) {}
 
  public updateStatus(newStatus: ShipmentStatus) {
    this.status = newStatus;
    this.logs.push(new TrackingLog(newStatus, new Date()));
    // Notify Observer
  }
}
 
class TrackingLog {
  constructor(public status: ShipmentStatus, public timestamp: Date) {}
}
 
interface ShippingStrategy {
  calculateCost(weight: number, distance: number): number;
}
 
class GroundShipping implements ShippingStrategy {
  calculateCost(weight: number, distance: number) {
    return weight * distance * 0.1;
  }
}

Deep Dive: Strategy Pattern for Cost Calculation

The Strategy Pattern is used to switch between different shipping speeds and costs dynamically. Whether it's "Same Day" or "Standard Ground," the calculation logic is decoupled from the Shipment class.


Wrap Up

A Logistics System tests your ability to handle Lifecycle Transfers (handing a package from one vehicle to a hub) and Cost Optimization. Using the Observer Pattern for tracking updates ensures that customers receive real-time notifications without polling the system.

[!TIP] Use the State Pattern to manage complex shipment transitions, preventing invalid state jumps (e.g., BOOKED directly to DELIVERED).