Head First Design Patterns only briefly describes the builder pattern in the appendix, without dedicating to it as much space as to other patterns.
Design Patterns: Elements of Reusable Object-Oriented Software treats it just like other design patterns.
Refactoring Guru also considers the builder pattern as a "true" pattern, without relegating it to an appendix.
Anyway, the first and the third sources put a strong focus, in my opinion, on how the pattern is useful to construct an object with just as many "ingredients" as the client wants.
In other words, a bad solution would be having a constructor like this
Ctor::Ctor(Param1* param1, Param2* param2, Param3* param3 /*, and many more */);
that needs to be called like this by a client who doesn't want to set the first two parameters
auto obj = Ctor(null, null, some_param3 /*, null, null */);
whereas the builder pattern would allow one to construct an object like this:
auto obj = Builder().setParam3(some_param3).build();
However, if all the builder pattern solved was the problem above, then I feel like named parameters would offer the same solution (and I'm not the only one to wonder about it). In turn, I'd have called the "builder pattern" the "named parameters pattern", that guides you to implement this feature in a language that lacks it.
Indeed, there are sources on the web that claim other languages don't need this pattern (or, if you like, the pattern is just less visible in them):
- here a convincing example of how Clojure's destructuring power just gives all you need as regards passing only the arguments you want to a constructor;
- a research engineer at Facebook (at least at the time he wrote this stuff), claims that Haskell's type classes and smart constructor are all you need to go on without the builder pattern, even though he doesn't go into any depth on the topic.
So, in view of the above point 1, my question is whether the builder pattern really offers more than a language feature like named parameters.
(I'm leaving point 2 out of the question because I'm not sure I've understood it.)
Aucun commentaire:
Enregistrer un commentaire