I saw a Builder pattern implementation in JAVA that differs from its C# version and the "classic" illustration by Gang of Four book.
C# - Gang of Four version
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
Product p1 = director.Make(b1);
Product p2 = director.Make(b2)
JAVA - link
Product p1 = new Product.Builder("p1").Name("Product1").Version("v1").Build();
Product p2 = new Product.Builder("p2").Name("Product2").Version("v2").Build();
I first saw the JAVA usage when I was getting into Android dev (as I come from .NET world) - to me the JAVA version is more elegant than its C# counterpart.
Now the underlying implementation differs of course, in JAVA example Builder is a nested class of Product, and its Build() method returns constructed instance of its parent class; where as C# interpretation above is more similar to Abstract Factory.
With everything mentioned above both are obviously 2 different patterns, although both are structural in nature. Does anyone know what the 2nd example is called in C#?
it's different patterns that you got.
First(C#) is building complex objects
Second(Java) builder technique of objects, without external builders.
So, this two patterns did different things, and have different scopes
I have seen it called a Static builder or a fluent builder. I would suggest "Static fluent builder" as the pattern includes three distinct elements.
I would further suggest that it is not a matter of C# vs. Java implementations of a single pattern, but two distinct patterns. You could probably say that either builder type, i.e. GOF/Classic or static-fluent could be coded in either language, as both languages support the necessary features. It could also be said that the static-fluid version is derived from the classic version and furthermore has an is-a relationship to the more general builder pattern. The GOF version could be implemented in most object oriented languages, but the static inner part of the newer pattern is not universally supported, however like the C# initializers mentioned in another answer, There are probably language specific ways to achieve the same result in other languages as well.
I am curious why one would not use a director object in the static-fluent builder pattern to cope with ordering and related issues. That is the reason for it's presence in the GOF pattern. The director can be static and the builder passed in as a parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With