Customized sandwiches must follow a correct, consistent assembly process; a Sandwich cannot be created until at least the bread and meat are set, and construction is separate from the Sandwich itself. Which pattern would you use? Draw its class diagram.
Show answer
Pattern: Builder. It separates the construction of a complex object from its representation and builds it step by step. SandwichBuilder (interface): setBread(), setMeat(), setCheese(), build(). CustomSandwichBuilder: stores the parts; build() refuses to create the Sandwich unless bread and meat are set (consistency). SandwichDirector: runs the steps in the correct order. Sandwich: the product (bread, meat, cheese). Diagram: Director → (directs) SandwichBuilder; CustomSandwichBuilder ⇢ realizes SandwichBuilder and creates Sandwich.