Design patterns to enjoy with frequently used Java libraries --Builder patterns

GoF design patterns are also hidden in the Java libraries that you use most often. It's easy to overlook the busy daily work, but once in a while, let's take a closer look at the beautiful design, which can be said to be a kind of art.

This art

import java.util.Locale;
import java.util.Locale.Builder;
...

Locale locale = new Builder()
    .setLanguage("ja")
    .setRegion("JP")
    .setScript("Latn")
    .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "ca-japanese")
    .build();

It is a Java Locale class instance generation scene that is often used when outputting the date and time, but when you watch it again, you will be fascinated by the beauty of the interface.

Points of appreciation

Locale The Builder class is used to create the instance. Set various properties with the setXXX method of this class, and then create an instance with the build method. I feel the beauty of the Locale instance, which makes me feel that it is immutable. Details will be explained below.

When not using the Builder pattern

In fact, the Locale class can also be instantiated directly with new. Rather, I usually write this way. I feel like this.

//The argument is language, country, variant
Locale locale = new Locale("ja", "JP", "JP");

So how do you add a new property to this Locale instance?

// XXX:There is no such method
locale.setScript("Latn");

You might come up with a way to set it with an instance method like this, but there is no such method. This is because the Locale class has an immutable design in which the contents cannot be changed once the instance is created. You can use the instance with confidence because the value will not be inadvertently changed elsewhere.

In that case, one way is to use a constructor with more arguments.

// XXX:No constructor takes 4 arguments
Locale locale = new Locale("ja", "JP", "JP", "Latn");

However, there is no such constructor. This method requires as many constructor types as there are properties, which makes the implementation unattractive to generate a class with a large number of properties.

When using the Builder pattern

By using the Builder pattern as in the code at the beginning, you can benefit from the following:

    1. (immutable object in this case) </ font> that can generate elaborate objects
  1. that can handle even if the number of setting items for the object increases (In this case, it is not necessary to create a constructor with more arguments even if the class properties increase) </ font>

Also,

  1. Arguments can be verified in the build method

That is also a feature of the Builder pattern. However, the argument verification can also be done in the constructor when directly newing, so it is irrelevant in this comparison.

Praise for the Builder pattern

Many experts have also praised the Builder pattern.

irxground

To create a complex object, it is useful to have a class dedicated to creation. http://qiita.com/irxground/items/d1f9cc447bafa8db2388

lang_and_engine

It is a pattern to make one shot with Dokan at the end after placing an order. A pattern in which settings and instructions can be passed one by one in advance to customize the final processing. It can be said that. You can say it one by one, so you don't have to stuff everything into the initialization process in the constructor. http://language-and-engineering.hatenablog.jp/entry/20120330/p1

Finally

It's the real pleasure of programmers to be able to enjoy intellectual enjoyment just by looking at a single line of code without having to go to the museum.

If you are an engineer who sympathizes with the artistry of the Builder pattern, please contact our recruiting staff (Qualysite Technologies Inc.). !!

Related article

Create an instance

-Design pattern to enjoy with Java library used frequently --Factory pattern --Design patterns to enjoy with frequently used Java libraries --Builder patterns -Design patterns to enjoy with frequently used Java libraries --Abstract Factory pattern

Simplify the interface

-Design patterns to enjoy with Java libraries that are often used --Facade patterns -Design pattern to enjoy with frequently used Java library --Adapter pattern

Leave it to another class

-Design patterns to enjoy with frequently used Java libraries --Template Method patterns -Design patterns to enjoy with frequently used Java libraries --Strategy patterns

Recommended Posts