1

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?

1 Answer 1

1

Its not a builder I don't think its just a factory method, ie a method for producing a fixed object.

The builder allows you to specify options to the object to be built before it is built, so that the instance built can be varied. A factory method just produces an instance of your object when called and is often static.

You could consider an approach where your builder class a static methods to return a default builder instance which has a lot of the options pre-configured, which you can then further modify. But if you are always wanting to return the same object then your factory method is fine.

Sign up to request clarification or add additional context in comments.

6 Comments

I had read the same Wiki article about the factory method pattern as referenced by you before I wrote my question. 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 that 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.
The "Definition" chapter of the same Wiki article states: "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. ..."
@krm yeah you are probably right its not a Factory Method in the true definition of the pattern, its just a small 'f' factory method, ie a method for producing an object. either way, I think the main point stands, that your class isn't a builder. I've updated the answer.
Thank you Sam. I started to answer using the comments below your answer, but came to the conclusion that it's too much text and added my thoughts about it to the main question. I also tend to the conclusion that it's some kind of a simple factory. How would you call the class in my case which contains this (and only this) static "factory" method? It's rather not a Builder, but can it be called MyFactory (the part "My" is of course a placeholder for something business specific).
I added +1 for your answer, but I'm still eager to hear what other people maybe have to say about this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.