Java Builder Pattern: Build Complex Objects Without Telescoping Constructors
Maya's bookstore app has grown since the last post. Checkout notifies customers, regions have their own senders and footers, and the shop is selling steadily. Now Maya wants a monthly sales report: how much each category sold, in which region, above which order value — delivered as a PDF, a CSV, or a web page, to whoever needs to see it. The first version of the report request is a single constructor with seven parameters, and every call site carries a comment explaining which argument is which — because without the comment, nobody can tell. Then the shop asks for an eighth option, a minimum order count, and the comment gets longer while the constructor gets wider. The question this post asks is the one the comment is already answering: how do you build an object with many optional parts, readably? Post 1 asked what varies? Post 2, Java Singleton Pattern: Enum, Lazy Holder & Double-Checked Locking Explained , asked how many? Post 3, Java Factory Pattern: Factory Method vs A...