← Back to PatternPip

FACTORY METHOD / THE WALKTHROUGH

Deliver the same package.
Let creation vary.

See why a creation hook can make a shared workflow easier to extend.

CHAPTER 1 OF 3

One package. One truck.

The delivery company only serves road routes. A small function checks the destination, creates a Truck and asks it to deliver the package.

FOLLOW THE CREATION

Where does the truck come from?

Try it yourself

Advance one step at a time. Watch where the function chooses, creates and uses a vehicle.

Package → Harbor depotSame input throughout the trace

STEP 1 / 5

Start with a package

Start with a fixed package for Harbor depot. Follow the code to find who chooses and creates its vehicle.

  1. 1Start with a package
  2. 2Enter workflow
  3. 3Create object
  4. 4Use object
  5. 5See the benefit
THE DISPATCH FUNCTION

deliverByRoad()

Validation, construction and delivery live in this function.

function deliverByRoad(input: Shipment): DeliveryReceipt {
  const shipment = validateShipment(input)
  const transport = new Truck()
  return transport.deliver(shipment)
}
THE PRODUCT · A SOFTWARE OBJECT

No vehicle object yet

The vehicle appears only when its constructor runs. Watch the creation step.

Step 1 of 5. Start with a fixed package for Harbor depot. Follow the code to find who chooses and creates its vehicle.

This is a guided replay of a real local model run. Each step waits for you; highlighted excerpts omit tracing callbacks and receipt bookkeeping. The complete source is in the lesson and quick reference.

Direct creation is enough for road delivery.

The function checks the destination, creates a Truck and asks it to deliver the package. No extension point is needed yet.

Follow the original function · TypeScript
export function deliverByRoad(
  input: Shipment,
  record: DeliveryRecorder = ignore,
): DeliveryReceipt {
  const shipment = validateShipment(input);
  record({
    phase: "validate",
    message: "The function checks the destination.",
  });
  const transport = new Truck();
  record({
    phase: "create",
    message:
      "The function constructs a Truck directly. There is no creation hook.",
  });
  const receipt = transport.deliver(shipment);
  record({ phase: "deliver", message: receipt.message });
  record({
    phase: "receipt",
    message: "A simple function is enough while only trucks are needed.",
  });
  return receipt;
}

Each chapter starts fresh. Theme and language changes preserve the current example.