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.
Appearance
FACTORY METHOD / THE WALKTHROUGH
See why a creation hook can make a shared workflow easier to extend.
CHAPTER 1 OF 3
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
Advance one step at a time. Watch where the function chooses, creates and uses a vehicle.
STEP 1 / 5
Start with a fixed package for Harbor depot. Follow the code to find who chooses and creates its vehicle.
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 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.
The function checks the destination, creates a Truck and asks it to deliver the package. No extension point is needed yet.
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.