This chapter concerns creating and destorying objects : when and how to create them, when and how to avoid creating them, how to ensure they are destoryed in a timely manner, how to manage any cleanup actions that must precede their destruction.…
Consider static factpry methods instead of construction 四个优点两个缺点 One advantage of static factory methods is that,unlike constructors,they have names. A second advantage of static factory methods is that,unlike constructors,they are not required to cr…
Item 1: Consider static factory methods instead of constructors Advantage: One advantage of static factory methods is that, unlike constructors, they have names. A second advantage of static factory methods is that, unlike constructors, they are not…
Obey the general contract when overriding equals 先了解equals 和 == 的区别,如果不重写equals, equals比较的仅仅只是是否引用同一个对象,==更多的是比较基本数值.当我们创建一些对象的时候. 当对象是单例模式不需要进行 equals 重写. Reflexive: For any non-null reference values x,x.equals(x) must return true. Symmetric:For any…
获得一个类的实例的传统方法是公共的构造方法,还可以提供一个公共的静态工厂方法(一个返回值为该类实例的简单静态方法), 例如Boolean(boolean 的封装类) public static Boolean valueOf(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; } 此方法将boolean的原始值转变成Boolean对象的引用. 注意:这里的静态工厂方法与设计模式中的工厂方法不一样.静态工厂方法有优缺点. 优点:①与构造方法相…
<Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 读书笔记 第1章 介绍 (Introduction) 第2章 创建和销毁对象(Creating and Destroying Objects) 第1条 考虑用静态工厂方式替代构造器(Consider static factory m…
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating and Destroying Objects Consider static factory methods instead of constructors Consider a builder when faced with many constructor parameters Enforce the s…
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Objects Consider static factory methods instead of constructors (factory方法可以拥有名称,可以避免重复创建,比如单例模式) Consider a builder when faced with many constructor parameter…
本文参考 本篇文章参考自<Effective Java>第三版第一条"Consider static factory methods instead of constructors" 另外参考了其它几篇文章的解读: https://www.cnblogs.com/dyj-blog/p/8867028.html https://blog.csdn.net/u014129886/article/details/89670049 前言 第一条的篇章来自"Creating…
Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of constructors 一个static factory method只不过是一个返回这个类的实例的static方法.与设计模式中的factory method是不同的概念.static factory methods有几点好处: 一.它们可以有自己的名字,而不像constructor只能与类名相同.一个好的名字…