《Effective Java第二版》阅读小记

2018-06-06  本文已影响0人  蒙多的菜刀
/**
 * @Description:遇到多个构造器参数时要考虑用构建器(Builder模式)
 */
public class NutritionFacts {

    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    // 静态内部类
    public static class Builder {
        // 必须的参数使用final修饰,切放在构造器中,为了保证一定要赋值
        // 可选的参数使用默认赋值,使用方法赋值,体现可选性


        // required parameters
        private final int servingSize;
        private final int servings;

        // optional parameters - initialized to default values
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

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

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

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

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

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    // 构造器私有化
    private NutritionFacts(Builder build) {
        this.servingSize = build.servingSize;
        this.servings = build.servings;
        this.calories = build.calories;
        this.fat = build.fat;
        this.sodium = build.sodium;
        this.carbohydrate = build.carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFacts{" +
            "servingSize=" + servingSize +
            ", servings=" + servings +
            ", calories=" + calories +
            ", fat=" + fat +
            ", sodium=" + sodium +
            ", carbohydrate=" + carbohydrate +
            '}';
    }

    public static void main(String... args) {
        NutritionFacts nf = new Builder(1, 0).calories(100).fat(50).sodium(22).carbohydrate(250).build();
        System.out.println(nf);
    }
}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description:单例的序列化
 * @Author: baitengpeng
 * @Date: 2018/5/25 下午2:05
 */
public class SingletonSerializable implements Serializable {
    private transient String name = "the singleton object";
    private transient int mumber = 13;

    private static SingletonSerializable singleton = null;

    static{
        singleton = new SingletonSerializable();
    }

    private SingletonSerializable(){
        // 防止通过反射破解单例
        if(null != singleton){
            throw new IllegalStateException("illegal operate");
        }
    }

    // 防止通过序列化反序列化破解单例
    private Object readResolve(){
        return singleton;
    }

    public static SingletonSerializable getInstance(){
        return singleton;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMumber() {
        return mumber;
    }

    public void setMumber(int mumber) {
        this.mumber = mumber;
    }

    @Override
    public String toString() {
        return "SingletonSerializable{" +
            "name='" + name + '\'' +
            ", mumber=" + mumber +
            '}';
    }

    public static void main(String...args)
        throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // 写对象
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(SingletonSerializable.getInstance());

        // 读对象
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        SingletonSerializable singletonSerializable = (SingletonSerializable) ois.readObject();

        // 比较读的对象和原先的对象是否相等
        System.out.println(SingletonSerializable.getInstance() == singletonSerializable);
        System.out.println(SingletonSerializable.getInstance());
        System.out.println(singletonSerializable);

        Class<SingletonSerializable> clazz = SingletonSerializable.class;
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for(Constructor c : constructors){
            c.setAccessible(true);
            System.out.println(SingletonSerializable.getInstance() == c.newInstance());
        }

//        SingletonSerializable objectFromReflection = clazz.newInstance();
//        System.out.println(SingletonSerializable.getInstance() == objectFromReflection);
//        System.out.println(objectFromReflection);
    }
}

/**
 * @Description:工具类,只包含静态方法和静态成员变量,这种类不希望被实例化
 * @Author: baitengpeng
 * @Date: 2018/5/25 下午3:11
 */
public class UtilityClass {
    // 将它的构造器声明为private,且为了防止反射,在构造器中抛出异常
    private UtilityClass(){
        throw new AssertionError();
    }
    // 静态方法和静态成员变量
    // ...
}
数字31的一个优良的性质是:乘法可以被位移和减法替代: 
31 * i == (i << 5) - i
上一篇 下一篇

猜你喜欢

热点阅读