Effective Java学习笔记---------创建和销毁对象

用静态工厂方法替代构造器(各有用处)

构造器方法

package codeTemplate.effectiveJava.bean;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Fruit {

    private  String color;
    private  String shape;
    private  int price;

    public Fruit(String color, String shape, int price) {
        this.color = color;
        this.shape = shape;
        this.price = price;
    }
    
    public Fruit() {
    }
}

静态工厂方法(可以起不同的函数名表示要创建怎样的对象)

package codeTemplate.effectiveJava.bean;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Fruit {

    private  String color;
    private  String shape;
    private  int price;

    private Fruit() {
    }

    public Fruit newInstance(String color, String shape, int price) {
        Fruit fruit = new Fruit();
        fruit.setColor(color);
        fruit.setShape(shape);
        fruit.setPrice(price);
        return fruit;
    }

    public Fruit newFruitWithShape(String color, String shape, int price) {
        Fruit fruit = new Fruit();
        fruit.setShape(shape);
        return fruit;
    }

}

遇到多个构造器参数时考虑使用构造器

package codeTemplate.effectiveJava.bean;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Fruit {

    private String color;
    private String shape;
    private int price;

    public Fruit(Builder builder) {
        this.color = builder.color;
        this.shape = builder.shape;
        this.price = builder.price;
    }

    public static class Builder {
        private String color;
        private String shape;
        private int price;

        public Builder() {
        }

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

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

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

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

用私有构造器或者枚举类强化Singleton属性

枚举类的单例模式

package codeTemplate.effectiveJava.bean;

public class Singleton {
    private Singleton(){
    }
    public enum SingletonEnum {
        SINGLETON;
        private Singleton instance;
        private SingletonEnum(){
            instance = new Singleton();
        }
        public Singleton getInstance(){
            return instance;
        }
    }
}

使用

public class EffectiveJavaDemo {
    public static void main(String[] args) {
        Singleton s1 = Singleton.SingletonEnum.SINGLETON.getInstance();
        Singleton s2 = Singleton.SingletonEnum.SINGLETON.getInstance();
        System.out.println(s1==s2);
    }

优先考虑依赖注入来引用资源(构造器的输入参数)

package codeTemplate.effectiveJava.tool;

public class SpellChecker {
    private final DictionaryOfI18 dictionary;
   	//dictionary为外部的资源(如字典.txt)
	public SpellChecker(DictionaryOfI18 dictionary) {
        this.dictionary = dictionary;
    }   
}

避免创建不必要的的对象(直接使用基本类型而不是它的包装类)

消除过期的对象引用

避免使用终结方式和清除方式

try-with-resource优先于try-finally

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值