[JAVA] static factory method part 1

I'm a new graduate Android engineer. I recently read Effective Java and found the keyword ** static factory method **.

static factory method

EffectiveJava says

Just a static method that returns an instance of the class

... apparently ...

For example, if you look at the code example of the Boolean class,

public static Boolean valueOf(boolean b) {
    return b ? Boolean.TRUE : Boolean.FALSE;
}

This method converts a basic data value called boolean to a Boolean object reference.

Normally, in order for a client to get an instance of a class, it is necessary to go through a public constructor, but if you use a static factory method, you can get an instance through a static method.

By the way, although the names are similar, they are different from the factory method, which is a kind of design pattern, so be careful not to confuse them.

Static factory method in singleton

Since the story of the design pattern came out, I will write it as a singleton pattern.

Guarantee that only one instance of that class will be created

There is a design pattern.

As a code to realize this, in Java, you can often see the following writing style.

public class Singleton {

    private static Singleton singleton = new Singleton();
    
    private Singleton() {
    }
   
    public static Singleton getInstance() {
        return singleton;
    }
}

In this case, getInstance () is a static factory method. Even if you don't know about the static factory method, many people know the singleton pattern, so I think this example will give you an image.

General name of the method

There is also a naming convention because if each person decides the method name as much as he / she likes, it will be indistinguishable from other static methods. Generally, there are the following method names.

Method name role
valueOf Returns an instance with the same value as the parameter. Type conversion.
of Method name without value of valueOf
getInstance Returns the instance specified by the parameter, but does not have the same value. In the case of a singleton, it takes no arguments and returns only one instance.
newInstance Similar to getInstance, except that all returned instances are separate.
getType Similar to getInstance, except that the factory method is in a different class than the target class.
newType Similar to newInstance, except that the factory method is in a different class than the target class.

If you have these static methods in your class, you can realize that they are static factory methods.

You can also see these methods in standard classes (such as Boolean above).

merit

So what makes you happy with the static factory method? It will be long, so I would like to summarize it in Part 2. Up to here for this time

reference

By Joshua Bloch Translated by Yoshiki Shibata Effective Java Second Edition

Recommended Posts

static factory method part 1
Factory Method Pattern
Factory Method pattern
Design pattern ~ Factory Method ~
Output about the method Part 1
20 Corresponds to static method invocation
No static method getFont error
Benefits of Java static method
Method
Introduction to Design Patterns (Factory Method)
static
Java beginner design pattern (Factory Method pattern)
Rails 5 Code Reading Part 1 ~ ActiveRecord new Method ~