规则1. 用静态工厂方法代替构造器

例子:

public class Example {
}
public class StaticFactory {
//valueOf/Of/getInstance/newIntance
private static volatile Example example = null;
public static Example newIntance(){
return new Example();
} public static Example getInstance(){
if (example == null) {
synchronized (Example.class) {
if (example == null) {
return new Example();
}
}
}
return example;
} public static void main(String[] args) {
Example example = StaticFactory.getInstance();
System.out.println(example.hashCode());
Example example1 = StaticFactory.newIntance();
Example example2 = StaticFactory.newIntance();
System.out.println(example1 == example2);
}
}

好处:(通过规则1,终于知道经常出现的getInstance()和newInstance()的区别)

1. 方法名有意义;

2. 可以创建新实例,也可以单例(控制对象的生产方式)

3. 可将多态应用在实例创建上

缺点:

1. 实例的构造方法为public/protected

2. 和普通静态方法一样

规则2. 用Builder实现多个参数的构造器

例子:

public class NutritionFacts {

    //required
private final int servingSize;
private final int servings;
//optional
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate; private NutritionFacts(Builder builder){
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
} public static class Builder{
//required
private int servingSize;
private int servings;
//optional(default)
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 calories){
this.calories = calories;
return this;
}
public Builder fat(int fat){
this.fat = fat;
return this;
}
public Builder sodium(int sodium){
this.sodium = sodium;
return this;
}
public Builder carbohydrate(int carbohydrate){
this.carbohydrate = carbohydrate;
return this;
} //build()
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
}
public static void main(String[] args) {
NutritionFacts nutritionFacts = new NutritionFacts.Builder(2,3)
.calories(12).carbohydrate(23).fat(1).sodium(8).build(); }

终于知道有些源码里出现静态内部类Builder的原理了。。

好处:

1. 避免构造函数有多个参数或者重载多个构造函数的情况

2. 如果想用JavaBean的形式解决,需要调用很多setter方法,容易出现线程不安全的问题

  比如:  

public class NutritionFactsSetter {

    private int servingSize;
private int servings;
private int calories;
private int fat; public int getServingSize() {
return servingSize;
}
public int getServings() {
return servings;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
public void setServingSize(int servingSize) {
this.servingSize = servingSize;
}
public void setServings(int servings) {
this.servings = servings;
} public void setCalories(int calories) {
this.calories = calories;
}
public void setFat(int fat) {
this.fat = fat;
}
public static void main(String[] args) {
NutritionFactsSetter factsSetter = new NutritionFactsSetter();
factsSetter.setCalories(0);
factsSetter.setServings(0);
factsSetter.setFat(20
);
}
}

  但是,通过Builder方式构建出的对象是不可改变的(在NutritionFacts类中,所有属性和方法以及构造函数都是private的),所以不存在线程不安全问题。

3. 一个Builder对象可以重复用来实例化对象。

4. 避免Class.newInstance()方法的异常处理情况

缺点:

1. 实例化语句显得有些冗长

规则3.

Effective Java总结的更多相关文章

  1. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

  2. 《Effective java》-----读书笔记

    2015年进步很小,看的书也不是很多,感觉自己都要废了,2016是沉淀的一年,在这一年中要不断学习.看书,努力提升自己!预计在2016年要看12本书,主要涉及java基础.Spring研究.java并 ...

  3. 《Effective Java》学习笔记——积累和激励

    从一个实际案例说起 国庆长假前一个礼拜,老大给我分配了这么一个bug,就是打印出来的报表数量为整数的,有的带小数位,有的不带,毫无规律. 根据短短的两个多月的工作经验以及猜测,最终把范围缩小到以下这段 ...

  4. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  5. effective java 读后感

    think in java  , effective java  这两本书一直都在java的生态圈中经久不衰.本来想着先翻过 think in java 这本大山,但是读到一半就放弃了.过长的篇幅,让 ...

  6. Effective java读书笔记

    2015年进步很小,看的书也不是很多,感觉自己都要废了,2016是沉淀的一年,在这一年中要不断学习.看书,努力提升自己 计在16年要看12本书,主要涉及java基础.Spring研究.java并发.J ...

  7. effective java —— 终结方法守卫者

    目录: effective java —— 终结方法守卫者 effective java 第2章:创建和销毁对象.第7条 : 避免使用终结方法.最后的“终结方法守卫者 (finalizer guard ...

  8. Effective Java 创建和销毁对象

    <Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...

  9. Effective Java

    Effective Java 创建和销毁对象---考虑用静态工厂方法代替构造器 构造器是创建一个对象实例最基本也最通用的方法,大部分开发者在使用某个class的时候,首先需要考虑的就是如何构造和初始化 ...

  10. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

随机推荐

  1. nodejs服务器anywhere简介

    一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g 执行 $ anywhere // or with port $ anywhere ...

  2. Centos6.5环境下安装SVN 整合Apache+SSL

    弄了两天,终于在服务器上初步搭建起来了SVN(版本1.8). 服务器系统:Centos6.5 64位,搭建过程中全部采用源码编译安装(configure/make/make install),推荐大家 ...

  3. 魅族MX3问题集锦

    我第一台智能机已经服役2年半了,已经满足不了现在日益庞大的APP,所以打算让他光荣退役.我觉得IPhone仍然是目前做的最好的手机,但是对于我来说好像没什么必要,尤其那土豪般的价格.而且我平时看视频居 ...

  4. 关于VS2012下安装破解文件Visual Assit X的一点说明

    今天在使用Visual Studio 2012的时候,编写代码的助手Visual Assit X突然提示我说,试用期已过,要求我输入一个注册码,我靠,这货不是几个月前已经破解了吗,怎么今天傻不愣登的提 ...

  5. [习题]日历(Calendar)控件的障眼法(.Visible属性),使用时才出现?不用就消失?

    原文出處  http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/02/calendar_icon_visible.aspx [习题]日历(Cal ...

  6. c语言内存分配-malloc

    malloc 原型:(原来返回类型是char) extern void *malloc(unsigned int num_bytes); 头文件: #include <stdlib.h> ...

  7. python时间-time模块

    time是python自带的模块,用于处理时间问题,提供了一系列的操作时间的函数. 以下说明针对于 python2.7,其他版本可能有所差异. 模块提供了两个种表示时间的格式: 1.时间戳,是以秒表示 ...

  8. javascript中方法调用与方括号[]

    看jquery时遇到一行: $(this)["removeClass"]("selected"); 这一行等同于下面的一行: $(this).removeCla ...

  9. 排序 选择排序&&堆排序

    选择排序&&堆排序 1.选择排序: 介绍:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始 ...

  10. sql server 2016 management studio没有的解决方式

    最近安装sql sever2016后发现没有 management studio管理工具,无法操作sql server,可以单独下载安装后即可. 下载地址: https://msdn.microsof ...