Course contentsShow
← Software Engineering
Lesson 245 of 1,9337. Creational Design PatternsFree lesson

Director Pattern and Construction Algorithms

Use a Director class to encapsulate complex construction sequences and reuse building algorithms across different builders.

Director Pattern and Construction Algorithms

What You'll Learn

You'll learn how a Director class manages the step-by-step construction process, letting you reuse complex building algorithms across different builders.

The Core Idea

Remember the Builder pattern separates what to build from how to build it. The Director takes this further by encoding the order and logic of construction steps into a reusable algorithm.

Think of building a house: a Builder provides the actual construction methods (buildWalls(), buildRoof()), but a Director is like the foreman who knows the exact sequence—foundation first, then walls, then roof. The same foreman can supervise different construction crews (builders) using the same process.

Why Use a Director?

Without a Director, client code must remember and repeat the construction sequence every time:

builder.setWalls();
builder.setRoof();
builder.setDoors();

If you build many objects with the same steps, this becomes repetitive and error-prone.

A Director encapsulates this sequence:

class HouseDirector {
    build(builder) {
        builder.setFoundation();
        builder.setWalls();
        builder.setRoof();
        builder.setDoors();
    }
}

Now you can reuse the same construction algorithm with different builders—one for wooden houses, another for brick houses—without duplicating the build logic.

Multiple Construction Algorithms

A Director can provide several construction methods:

  • buildMinimalHouse() - just walls and roof
  • buildLuxuryHouse() - includes foundation, walls, roof, doors, windows, pool

Each method is a named construction algorithm clients can choose from.

The Relationship

  • Builder = knows how to build individual parts
  • Director = knows which parts to build and in what order
  • Client = chooses a Director algorithm and Builder implementation

Key Takeaway: The Director pattern encapsulates multi-step construction sequences as reusable algorithms, separating the building process from both the builder implementation and the client code.