/ Java

Introducing ByggarMonster

It is often considered good coding practice to create and use pure immutable data objects. That means all members has to be set in the constructor. This often results in code like this.

Data data = new Data("s1","s2");

It is not easy to read such code since you don't immediately see which string corresponds to which attribute. If you create the class via a builder it can look like this.

Data data = new DataBuilder()
 .withFile("s1")
 .withCharset("s2")
 .build();

Sometimes you need an object that is a copy of another object, but with a small adjustment. Like this.

Data data = new Data("s3",otherData.getS2());

The more parameters, the harder it gets to read. Also if the object is not immutable you may have code like this.

String oldS2 = data.getS2();
data.setS2("newValue");
someFunction(data);
data.setS2(oldS2);

This code is hard to read and is likely to contain mistakes. A builder can solve this by providing a "from" method, like this.

someFunction(
 new DataBuilder()
  .from(data)
  .withS2("newValue")
  .build()
);

ByggarMonster

ByggarMonster translates to BuilderPattern and it will create builders from your existing data classes without any modification of them. As long as your data classes are pure data holders ByggarMonster will give you a nice builder for it.

Here is a sneak peak, check out GitHub for more examples. Or try the online builder at http://byggarmonster.bjurr.se.

package se.byggarmonster.sandbox;
public class Example {
 private final String member1;

 public TwoFinalAttributesSrc(final String member1) {
  this.member1= member1;
 }

 public String getMember1() {
  return member1;
 }
}
package se.byggarmonster.sandbox;

public class ExampleBuilder {
 private String member1;

 public Example build() {
  Example instance = new Example(member1);
  return instance;
 }

 public ExampleBuilder from(final Example from) {
  ExampleBuilder instance = new ExampleBuilder();
  instance.member1 = from.getMember1();
  return instance;
 }

 public ExampleBuilder withMember1(final String member1) {
  this.member1 = member1;
  return this;
 }
}