DEV Community

NOOB
NOOB

Posted on

LLD:15-Order Management System

Order Management System - State Pattern Implementation

This is an implementation of the State design pattern for an e-commerce Order Management System. By utilizing this pattern, an order dictates its own lifecycle (Created, Paid, Shipped, Delivered) and strictly enforces business rules without relying on messy if/else or switch statements evaluating an "order status" flag.

Problem Statement

Building the backend flow for an e-commerce order that transitions through four sequential states: Created, Paid, Shipped, and Delivered. The challenge is ensuring that illegal operations—like attempting to ship an unpaid order or paying for an order that has already been delivered—are gracefully blocked based on the current state.

Key Challenge:

  • If ship() is called in the Created state → "ERROR: Cannot ship an unpaid order."
  • The happy path must flow perfectly: Created -> pay() -> Paid -> ship() -> Shipped -> deliver() -> Delivered.
  • The Context (Order) should not contain the validation logic; the states themselves must handle their own localized rules and transitions.

Class Diagram

                            +----------------------+
                            |    OrderManagement   | <-------------------+
                            +----------------------+                     |
                            | - currentState       |                     |
                            +----------------------+                     |
                            | + setState()         |                     |
                            | + pay()              |                     |
                            | + ship()             |                     |
                            | + deliver()          |                     |
                            +----------+-----------+                     |
                                       |                                 |
                                       | (Delegates to)                  |
                                       v                                 |
                            +----------------------+                     |
                            |        State         |                     |
                            |      (Interface)     |                     |
                            +----------------------+                     |
                            | + pay()              |                     |
                            | + ship()             |                     |
                            | + deliver()          |                     |
                            +----------+-----------+                     |
                                       ^                                 |
                                       | (Implements)                    |
            +--------------------------+--------------------------+      |
            |                          |                          |      |
 +----------+----------+    +----------+----------+    +----------+------+-+
 |    CreatedState     |    |      PaidState      |    |   ShippedState    |
 +---------------------+    +---------------------+    +-------------------+
 | - orderManagement   |--->| - orderManagement   |--->| - orderManagement |
 | + pay()             |    | + pay()             |    | + pay()           |
 | + ship()            |    | + ship()            |    | + ship()          |
 | + deliver()         |    | + deliver()         |    | + deliver()       |
 +---------------------+    +---------------------+    +-------------------+
              (And DeliveredState follows the exact same pattern!)
Enter fullscreen mode Exit fullscreen mode

Implementation

package state.orderManagementSystem;

public class OrderManagementSystem {

    /**
     * 1. The State Interface
     * Defines the core actions that can be performed on an order.
     */
    interface State {
        void pay();
        void ship();
        void deliver();
    }

    /**
     * 2. The Context (OrderManagement)
     * Maintains a reference to the current state and delegates actions.
     */
    static class OrderManagement {
        State createdState;
        State paidState;
        State shippedState;
        State deliveredState;

        State currentState;

        public OrderManagement() {
            // Pass 'this' so states can talk back to the Context to change states
            createdState = new CreatedState(this);
            paidState = new PaidState(this);
            shippedState = new ShippedState(this);
            deliveredState = new DeliveredState(this);

            // Order starts in the Created state
            currentState = createdState;
        }

        public void setState(State state) {
            this.currentState = state;
        }

        public void pay() {
            currentState.pay();
        }

        public void ship() {
            currentState.ship();
        }

        public void deliver() {
            currentState.deliver();
        }
    }

    /**
     * 3. Concrete State: Created
     * Order is placed, waiting for payment.
     */
    static class CreatedState implements State {
        private final OrderManagement orderManagement;

        public CreatedState(OrderManagement orderManagement) {
            this.orderManagement = orderManagement;
        }

        @Override
        public void pay() {
            System.out.println("Payment successful! Order is now Paid.");
            orderManagement.setState(orderManagement.paidState); // Transition
        }

        @Override
        public void ship() {
            System.out.println("ERROR: Cannot ship an unpaid order.");
        }

        @Override
        public void deliver() {
            System.out.println("ERROR: Cannot deliver an unpaid order.");
        }
    }

    /**
     * 3. Concrete State: Paid
     * Payment is successful, waiting for dispatch.
     */
    static class PaidState implements State {
        private final OrderManagement orderManagement;

        public PaidState(OrderManagement orderManagement) {
            this.orderManagement = orderManagement;
        }

        @Override
        public void pay() {
            System.out.println("ERROR: Order is already paid.");
        }

        @Override
        public void ship() {
            System.out.println("Order dispatched! Order is now Shipped.");
            orderManagement.setState(orderManagement.shippedState); // Transition
        }

        @Override
        public void deliver() {
            System.out.println("ERROR: Cannot deliver an order that hasn't shipped.");
        }
    }

    /**
     * 3. Concrete State: Shipped
     * The order is on the truck, waiting for delivery.
     */
    static class ShippedState implements State {
        private final OrderManagement orderManagement;

        public ShippedState(OrderManagement orderManagement) {
            this.orderManagement = orderManagement;
        }

        @Override
        public void pay() {
            System.out.println("ERROR: Order is already paid.");
        }

        @Override
        public void ship() {
            System.out.println("ERROR: Order is already shipped.");
        }

        @Override
        public void deliver() {
            System.out.println("Order dropped off! Order is now Delivered.");
            orderManagement.setState(orderManagement.deliveredState); // Transition
        }
    }

    /**
     * 3. Concrete State: Delivered
     * The order is complete. No further actions allowed.
     */
    static class DeliveredState implements State {
        private final OrderManagement orderManagement;

        public DeliveredState(OrderManagement orderManagement) {
            this.orderManagement = orderManagement;
        }

        @Override
        public void pay() {
            System.out.println("ERROR: Order is already complete.");
        }

        @Override
        public void ship() {
            System.out.println("ERROR: Order is already complete.");
        }

        @Override
        public void deliver() {
            System.out.println("ERROR: Order is already delivered.");
        }
    }

    /**
     * 4. Main Driver
     */
    public static void main(String[] args) {
        System.out.println("---- Order Management System ----");

        OrderManagement order = new OrderManagement();

        System.out.println("\n--- Attempt 1: Trying to ship before paying ---");
        order.ship();

        System.out.println("\n--- Attempt 2: Happy Path ---");
        order.pay();
        order.ship();
        order.deliver();

        System.out.println("\n--- Attempt 3: Action after completion ---");
        order.pay();
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Strict Business Logic Pipeline: The pattern perfectly models a one-way sequential pipeline (Created -> Paid -> Shipped -> Delivered) where skipping steps is inherently impossible.
  • Localized Error Handling: Instead of the Context checking if (status == CREATED), the CreatedState naturally knows how to reject ship() and deliver() requests.
  • No Complex Conditionals: Adding a new step (e.g., RefundedState) requires creating a new class rather than modifying massive switch blocks in the core OrderManagement logic.
  • Self-Contained Transitions: Each state is responsible for handing the baton to the next logical state via orderManagement.setState().

How It Works

  1. Initialization: When OrderManagement is instantiated, it initializes all possible states and defaults its currentState to CreatedState.
  2. Invalid Action: If the client calls ship() while in CreatedState, the request is passed to CreatedState.ship(), which outputs an error and prevents any transition.
  3. Happy Path Transition: Calling pay() on CreatedState executes the payment logic and transitions the context to PaidState. Subsequent calls to ship() and deliver() cascade through the remaining states.
  4. Terminal State: Once it reaches DeliveredState, all core actions (pay, ship, deliver) act as dead ends, preventing further manipulation of a completed order.

Sample Output

---- Order Management System ----

--- Attempt 1: Trying to ship before paying ---
ERROR: Cannot ship an unpaid order.

--- Attempt 2: Happy Path ---
Payment successful! Order is now Paid.
Order dispatched! Order is now Shipped.
Order dropped off! Order is now Delivered.

--- Attempt 3: Action after completion ---
ERROR: Order is already complete.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)