I know and often use the classic "Builder" design pattern which is described among others here. However, I have now a case in which I would like to use something like a Builder pattern, but in this particular case I do not have to set any parameters in my "Builder", because all values which are used by the "Builder" are constant and do not have to be set dynamically. I implemented my "Builder" now as a classic Builder, like this (the code is simplified):
final class MyBuilder {
private static final String FOO = "foo";
private static final String BAR = "bar";
private List<MyResult> results = new ArrayList<>();
MyBuilder() {
// empty constructor
}
List<MyResult> build() {
results.add(createFoo());
results.add(createBar());
return results;
}
private MyResult createFoo() {
...
}
private MyResult createBar() {
...
}
...
}
This builder is used like this:
MyBuilder resultsBuilder = new MyBuilder();
List<MyResult> results = resultsBuilder.build();
or can be alternatively used like this:
List<MyResult> results = new MyBuilder().build();
Due to the fact that unlike a classic Builder this particular "Builder" doesn't expect any input parameters (no setters of the builder have to be invoked), it could provide a static method which will create an instance of it and return the final result, so that the builder could be used like this:
List<MyResult> results = MyBuilder.build();
The "builder" could contain the following code in such case (the build() method is static):
final class MyBuilder {
private static final String FOO = "foo";
private static final String BAR = "bar";
private List<MyResult> results = new ArrayList<>();
private MyBuilder() {
// hidden constructor
}
static List<MyResult> build() {
MyBuilder builder = new MyBuilder();
return builder.build();
}
private List<MyResult> buildResults() {
results.add(createFoo());
results.add(createBar());
return results;
}
private MyResult createFoo() {
...
}
private MyResult createBar() {
...
}
...
}
Is it still a Builder? Is such a modification of the Builder pattern OK in your opinion? Does a different design pattern exist which could be more suitable in this particular case?
I had read about the factory method pattern, but I came to the conclusion that my case is not a factory method pattern, because it doesn't use polymorphism. See the first sentence of the aforementioned article:
"In class-based programming, the factory method pattern is a creational pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created."
I specify the exact class of the objects which will be created by my static method. See also the Definition chapter in the aforementioned article:
"The factory method pattern should not be confused with the more general notion of factories and factory methods. The factory method pattern is the best-known use of factories and factory methods, but not all uses of factory methods are examples of the factory method pattern – only when inheritance is involved (a class implementing an interface, or derived class, implements a factory method) is it an example of the factory method pattern. More basic uses of factories are not examples of the factory method pattern, and may instead be referred to as the factory pattern or a simple factory; these are discussed at factory."
So maybe this is a kind of a simple factory (not to confuse with the factory method pattern)? What do you think about it?
How should the class "MyBuilder" (where "My" is a placeholder for a business specific name) be called? Maybe MyFactory?