...
/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...
We'll cover the following...
Solution explanation
Lines 3–10: We initialize the builder’s full internal state with all possible fields and a
readyflag, which tracks whether both required fields—usernameandpassword—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 (
withEmail,withPhone,withAvatar) call_assertReady()before execution, throwing an error if the builder isn’t ready. This enforces ...