...

/

Solution: Enforce a Required Build Sequence in a Sign-Up Form

Solution: Enforce a Required Build Sequence in a Sign-Up Form

Implement a builder that tracks internal state and prevents access to optional methods or .build() until all required fields are explicitly set.

We'll cover the following...

Solution explanation

  • Lines 3–10: We initialize the builder’s full internal state with all possible fields and a ready flag, which tracks whether both required fields—username and password—have been set.

  • Lines 13–23: The .withUsername() and .withPassword() methods update their respective fields and invoke _checkReady() to enable optional methods once both are provided.

  • Lines 25–41: Optional setters (withEmailwithPhonewithAvatar) call _assertReady() before execution, throwing an error if the builder isn’t ready. This enforces ...