effective java reading notes chapter 1: creating and destroying objects

Article 1: Consider replacing constructors with static factory methods
1. The first advantage different from constructors is that they have names and are easy for users to call, especially for constructors with different parameters.
2. It is not necessary to create a new object in each invocation. It can return the same object for repeated invocation, while reducing the repetitive creation of objects and saving system overhead.
3. They can return any subclass object of the original return type, which is more flexible.
The sample code is as follows:

package main.java.factory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Paul
 * @version 0.1
 * @date 2018/4/21
 */
public class Services {
    //Construction method private
    private Services() {
    }

    private static final Map<String, Factory> providers = new ConcurrentHashMap<>();
    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    public static void registerDefaultProvider(String name, Factory p) {
        registerProvider(DEFAULT_PROVIDER_NAME, p);
    }

    private static void registerProvider(String defaultProviderName, Factory factory) {
        providers.put(defaultProviderName, factory);
    }

    //Create services with default factories
    public static service newInstance() {
        return newInstance(DEFAULT_PROVIDER_NAME);
    }

    //Create services with specified factories
    public static service newInstance(String name) {
        Factory factory = providers.get(name);
        if (factory == null) {
            throw new IllegalArgumentException("No factory registered with name" + name);
        }
        return factory.newService();
    }

}

interface Factory {
    service newService();
}

interface service {
    //Service Method Interface
}

The disadvantage of this method is that if there is no public or protect ion constructor in the class, it cannot be subclassed. Moreover, the static factory method is virtually the same as other static methods in the class, so it may be difficult to distinguish them.

Article 2: Consider using builder instead of constructor when there are too many member variables
It provides another way to construct javaBean pattern, which guarantees thread security and good readability.

package main.java.factory;

/**
 * @author Paul
 * @version 0.1
 * @date 2018/4/22
 */
public class Student {
    private final String name;
    private final int age;
    private final int code;
    private final String clazz;

    public static class Builder {
        private final String name;
        private final int age;
        private int code = 0;
        private String clazz = null;

        public Builder(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Builder code(int code) {
            this.code = code;
            return this;
        }

        public Builder clazz(String clazz) {
            this.clazz = clazz;
            return this;
        }

        public Student bulid() {
            return new Student(this);
        }
    }

    public Student(Builder builder) {
        name = builder.name;
        age = builder.age;
        code = builder.code;
        clazz = builder.clazz;
    }

    public static void main(String[] args) {
        //Client Call
        Student student=new Builder("paul", 12).code(2).clazz("Class one").bulid();
    }
}

Article 3: Enhance singleton with private constructors or enumerations and ensure that it is not instantiated
The construction method is privatized and an object is constructed by publis static final inside the class for external invocation to ensure global uniqueness.

Article 4: Avoid creating unnecessary objects
For final classes that provide both constructors and static factory methods, static factory methods are preferred to reuse final objects.

Article 5: Avoid the use of termination methods and eliminate obsolete object references

Keywords: Java

Added by imran.rajani on Wed, 15 May 2019 03:00:17 +0300