The first chapter of effective java reading notes creates and destroys objects

Item 1: Consider using static factory methods instead of constructors
1. The first advantage of being different from constructors is that they have names, which are convenient for users to call, especially when compared to constructors with different parameters.
2 .It is not necessary to create a new object every time it is called, the same object can be returned for repeated calls, and the repeated creation of objects can be reduced, saving system overhead.
3. They can return any subclass object of the original return type, which appears 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 {
    //构造方法私有
    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);
    }

    //用默认工厂创建服务
    public static service newInstance() {
        return newInstance(DEFAULT_PROVIDER_NAME);
    }

    //用指定工厂创建服务
    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 {
    //服务方法接口
}

Its disadvantage is that if the class does not contain a public or protect constructor, it cannot be subclassed, and the static factory method is actually no different from other static methods in the class, so it may be difficult to distinguish.

Article 2: Consider using builder instead of constructor when there are too many member variables
Provides another construction method other than the javaBean pattern, which ensures thread safety 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) {
        //客户端调用
        Student student=new Builder("paul", 12).code(2).clazz("一班").bulid();
    }
}

Article 3: Use private constructors or enumerations to strengthen singleton, and ensure that it is not instantiable.
Make the constructor private, and at the same time construct an object for external calls through publis static final inside the class 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. In order to reuse final objects

Item 5: Avoid finalizers and eliminate expired object references

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724051&siteId=291194637