Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the builder pattern in a for loop

So I came across some code that looks like this:

Polygon polygon = Polygon.Builder()
                .addVertex(new Point(38.085255f, -122.734590f))
                .addVertex(new Point(37.513400f, -122.726350f))
                .addVertex(new Point(37.044617f, -122.413239f))
                .addVertex(new Point(37.121307f, -121.765046f))
                .addVertex(new Point(37.497051f, -121.707368f))
                .addVertex(new Point(37.812351f, -121.905122f))
                .addVertex(new Point(37.899094f, -121.740327f))
                .addVertex(new Point(37.987900f, -121.877656f))
                .addVertex(new Point(37.886089f, -122.034211f))
                .addVertex(new Point(38.085247f, -122.366548f))
                .build();

This simply adds the points with float coordinates to an array and then at the end builds the polygon. Anyway, my question is if there is any easy way where I could loop through this addVertex process without having to change the basic structure of the process?

Basic idea of what I'm trying to do is:

for(int i = 0; i < vertices.length; i++) {
    polygon.Builder.addVertex(new Point(vertices[i].getX(), vertices[i].getY());
}
polygon.Builder().build();

I tried to generalize this example as much as possible and hopefully I didn't add any confusion in the process.

like image 867
whla Avatar asked Sep 17 '14 22:09

whla


People also ask

What is builder pattern used for?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.

What is Builder () in Java?

Builder is a creational design pattern, which allows constructing complex objects step by step. Unlike other creational patterns, Builder doesn't require products to have a common interface. That makes it possible to produce different products using the same construction process.

What are the consequences of applying the Builder design pattern?

Here are key consequences of the Builder pattern: It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product.


1 Answers

With this method-chaining builder pattern, the key is to recognize that each call to addVertex returns a (new) builder. To loop this, you can repeatedly overwrite the current builder with a new one each iteration.

PolygonBuilder builder = Polygon.Builder();

for (int i = 0; i < vertices.length; i++) {
    builder = builder.addVertex(new Point(vertices[i].getX(), vertices[i].getY());
}

Polygon polygon = builder.build();

This works whether each addVertex call returns a new builder or whether it returns the same builder each time. Either way.

like image 118
John Kugelman Avatar answered Sep 19 '22 10:09

John Kugelman