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

Thursday, 24 May 2018

Hello World with Maven

Getting started with Maven is slightly complicated, so I thought I'd write down some links1 2 on how to get up and running quickly with a simple HelloWorld Java application with Maven archetypes.

That and I wanted to tell how to upgrade the Java version used for the Maven project. There seem to be different ways to do it. Some easy, some hard.

Groupidorg.apache.maven
Artifactidthe name of the jar without version, for example maven, commons-math
Version2.0
mvn archetype:generate -DgroupId=com.mrbear -DartifactId=mrbear -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Verify that this is the latest archetype version4.

Upgrading to Java 8

One way is to add the versions to the properties in the pom.xml, which is easiest.

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

Another way is to change the maven compiler plugin to use the new version, which is harder.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

References

[1] Apache Maven Project - Creating a simple java application
https://maven.apache.org/plugins-archives/maven-archetype-plugin-1.0-alpha-7/examples/simple.html
[2] Apache Maven Project - Maven in 5 Minutes
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
[3] Apache Maven Project - Setting the -source and -target of the Java Compiler
https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
[4] Apache - Maven Archetype Quickstart Summary
http://maven.apache.org/archetypes/maven-archetype-quickstart/summary.html