Builder 00:00:00 Make a prediction You will distinguish an incomplete construction process from a valid finished value and test whether the finished product is independent of its builder. A request builder collects headers in a mutable list. You build one request, then add another header to the builder. Should the finished request gain that header? No, if it promises a snapshot. Copy or transfer the collection when building. Final fields do not prevent a shared mutable list from changing afterwards. 00:00:33 A kite, spar by spar A kite starts as spars, string and a sail on a workbench. It is acceptable for a half-built kite to lack a bridle; sending it into the wind in that state is not. The builder checks the bridle before handing the kite over. Let the builder accumulate named choices and incomplete intermediate state. Let build validate the combination and publish a complete product with clear ownership. The Builder is a creational design pattern. A separate object receives the parts of a product step by step. When all is there, build creates the product. Constructors with many parameters are read by position. Two values of the same type can be swapped and nobody notices. Overloads for optional parts multiply. 00:01:21 Finish a valid request A report download request has a required URL and optional headers and timeout. Positional arguments make it easy to pass a timeout where a retry count belongs. Set named choices on a temporary builder. When build is called, validate that the URL exists and the timeout is positive. Copy the header collection into the resulting request. Later changes to the builder must never change an already scheduled download. First try building before providing a required address, then supply it and set a positive timeout. Only the second build should return a usable request. Change a builder option afterward and inspect the previous request; the completed value must have captured its own configuration. 00:02:08 A meal order under construction The customer can choose a sandwich, a side and a drink in several steps. The partially completed builder can be missing the sandwich. The submitted kitchen order cannot. A test that tries to submit without the required item should fail before the kitchen sees the order. Choose the sandwich filling before the bread and then reverse the order. Both selection paths can yield the same valid meal. Try finishing with bread still missing. The builder may represent incomplete work, but the completed meal should not expose that intermediate state. A builder that returns an invalid product and asks the caller to remember a separate validation step has moved the bug without removing it. For a small value with two obvious required fields, a direct constructor can express the same rule more clearly. OkHttp Request.Builder. A request is created from its builder, which must have a url. Request.newBuilder returns a builder filled with the request's values, the way back. java.net.http.HttpRequest. The documentation says that a built request is immutable and can be sent several times, that build throws IllegalStateException if no URI was set, and that copy duplicates a builder. 00:03:29 Try an interview follow-up When would you remove a builder? When all values are known in one place, rules fit into the constructor, and the language has named arguments. Then it repeats every field. Keep it where parts arrive over time, rules need collected messages, or a public interface must grow. A catering order needs a delivery date and at least one meal. A builder can accept options in several sequences. Try to finish before setting the delivery date. Reject the incomplete order at build time. Also verify that editing the builder for a second order cannot change the first completed order. 00:04:08 What to take with you Collect named decisions in a changeable builder, check them together, create an immutable product once. Steps return the builder, build returns the product. Form of one value: in the step. Rules between parts: in build and the constructor. Facts about the world: in the service. Report all problems at once.