简体   繁体   中英

Builder Pattern Implementation C# vs. JAVA

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

They are both considered the Builder pattern as they are just two ways of accomplishing the same thing. In your post, you cited the GoF example in the C# snippet. The Java example comes from Joshua Bloch's Effective Java. The key reason for the difference is encapsulation. Another is the fluent DSL style interface you have for building the object.

There is nothing at all preventing you from implementing the Builder pattern in C# the way Bloch describes it (well, almost nothing at all). In Java, an outer class can access the private fields of a nested class. This is not possible in .NET, so you have to follow a different approach. This is typically done by copying the object state from the builder into the new instance in the Build method.

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.

  1. It builds a complex product object.
  2. It has a fluent interface .
  3. It Is implemented as a static inner class.

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM