Showing posts with label builder pattern. Show all posts
Showing posts with label builder pattern. Show all posts

Monday, 4 June 2018

Can I use a builder to build many objects?

I recently questioned if it is okay to use a builder to make multiple objects. The software was designed in such a way that this is possible, but I hesitated because I did not know if it was a good idea.

I know for a fact that a lot of builders in our software, might not be suitable for this use case, as they have too many dependencies (that shouldn't be there) on using the proper sequence.

That said, the following quotes are good to take to heart when designing builders1 2:

“The Builder pattern is flexible. A single builder can be used to build multiple objects. The parameters of the builder can be tweaked between object creations to vary the objects. ”
“The builder object is responsible for constructing a valid object but the object is not constructed until you call the build() method. This means the same builder can be used multiple times to construct completely different objects.”

I especially like the last quote.

References

[1] StackOverflow - How to use a single builder to build multiple objects?
https://stackoverflow.com/questions/14429043/how-to-use-a-single-builder-to-build-multiple-objects
[2] Effective Java 2
Joshua Blog

Sunday, 27 October 2013

Trainwreck vs. Method Chaining

I thought it worth while to expound on the differences between a train wreck and method chaining.

Whereas a trainwreck is a bad idea, method chaining is an excellent idea. This blog will try to explain why these two are polar opposites on the good/bad scale, even though the syntax differs very little.

Trainwreck


A trainwreck is a bad idea. It provides an Object A with in depth knowledge of several other objects. Knowledge that should be contained in the individual objects instead of Object A.

In code it would look like follows:

public class A
{
    public void someMethod(B b)
    {
        b.getC().getD().getE().doThing();
    }
}

A Trainwreck breaks the Law of Demeter[1] (and everyone knows, if you break a law, you have to go to jail).

A solution would be to just tell B to do it, and let it figure it out.
public class A
{
    public void someMethod(B b)
    {
        b.doThing();
    }
}

Method Chaining


Method chaining is an excellent idea. The methods used always return the Object itself. It's a good way of constructing an Object without having to resort to multiple different Constructor methods with varying (large amount of) parameters.
public class Person
{

    private long id;
    private String firstName;
    private String lastName;
    private String address;
    private String telephone;
    private String title;

    public Person(long id)
    {
        this.id = id;
    }

    public Person setFirstName(String firstName)
    {
        this.firstName = firstName;
        return this;
    }

    ...
}

public static void main(String[] args)
{
    Person mrBear = (new Person(1l)).setFirstName("B.").
              setLastName("Bear").setTitle("Mr.");
}

Method Chaining does not break the Law of Demeter[1]. It doesn't even talk to friends (in this case), but only to itself.

Here I have used a simplified example. Mostly you'd create a PersonBuilder to make Persons.[4]

For some great uses of method chaining check out [2] and [3].

References

[1] Glossary
http://randomthoughtsonjavaprogramming.blogspot.nl/p/glossary.html
[2] FluentInterface
http://martinfowler.com/bliki/FluentInterface.html
[3] Expression Builders
http://martinfowler.com/bliki/ExpressionBuilder.html
[4] Too Many Parameters in Java Methods, Part 3: Builder Pattern
http://marxsoftware.blogspot.nl/2013/10/too-many-parameters-in-java-3-builder-pattern.html