1.Creating and Destroying Objects——Effective Java 2nd Ed学习笔记

Item 1: Consider static factory methods instead of constructors


1.One advantage of static factory methods is that, unlike constructors, theyhave names.

有时候,一个类有几个不同的构造函数,各个构造函数只能靠参数来区分,编写程序和阅读程序的时候构造子的意义都不明确。而是用静态工厂方法可以明确的知道该构造子的含义,比如BigInteger(int, int, Random),这个构造子返回的可能是一个素数,但是在代码上并没有体现出来,使用BigInteger.probablePrime()静态工厂方法的意义就明确得多了。

2.A second advantage of static factory methods is that, unlike constructors,they are not required to create a new object each time they’re invoked.

有时候并不需要每次都创建一个新的对象实例,就可以使用静态工厂方法,避免了创建对象的性能损失。比如Boolean.valueOf(boolean),使用该方法不会重新创建一个Boolean对象,它的代码如下,很明显,这比每次都创建一个新对象在性能上要好得多。This technique is similar to theFlyweight pattern [Gamma95, p. 195].

public static Boolean valueOf(boolean b) {

return b ? Boolean.TRUE : Boolean.FALSE;

}

而且有时,静态工厂方法还可以用来保证没有两个相等的对象同时存在,所以在这种情况下可以直接使用a==b判断两个对象是否相等,比使用equals方法效率高。3.A third advantage of static factory methods is that, unlike constructors,they can return an object of any subtype of their return type.

a) One application of this flexibility is that an API can return objects without making their classes public.

For example, the Java Collections Framework has thirty-two convenience implementations of itscollection interfaces, providing unmodifiable collections,synchronized collections, and the like.Nearly all of these implementations are exported via static factory methods in one noninstantiable class (java.util.Collections).The classes of the returned objects are all nonpublic.这样使用者完全不需要了解那些类就可以使用它们,而且这也使得使用者可以针对接口编程。

b) Not only can the class of an object returned by a public static factory method be nonpublic, but the class can

vary from invocation to invocation depending on the values of the parameters to the static factory.java.util.EnumSet没有公共的构造子,只有静态工厂方法,该方法实际上返回EnumSet的两种不同的实现(这一点使用者完全不需要了解):如果EnumSet大小小于64,就返回RegularEnumSet实例(当然它继承自EnumSet),这个EnumSet实际上至用了一个long来存储这个EnumSet。如果 EnumSet大小大于等于64,则返回JumboEnumSet实例,它使用一个long[]来存储。这样做的好处很明显:大多数情况下返回的RegularEnumSet效率比JumboEnumSet高很多。

c) The class of the object returned by a static factory method need not even exist at the time the class containingthe method is written.这是service provider frameworks的基础。

比如说Java Database Connectivity API 就是一个 service provider frameworks,在编写JDBC的时候,各个厂商所提供的JDBC驱动根本就不存在,但是JDBC API却能返回这些厂商的JDBC对象。service provider frameworks有3个基本的组件:1.服务接口(service interface),服务的提供者必须实现该接口,在JDBC中是Driver接口,2. 服务提供者注册(providerregistration)API,JDBC中即DriverManager.registerDriver()3.服务访问(service access)API,JDBC中即DriverManager.getConnection

d) A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized typeinstances.(这一点已经没有优势了,因为JDK1.7对此做了简化)

Map<String, List<String>> m = new HashMap<String, List<String>>();

==> Map<String, List<String>> m = HashMap.newInstance();

4.The main disadvantage of providing only static factory methods is that classes without public orprotected constructors cannot be subclassed.

5.A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.而且,在Javadoc中,静态工厂方法不如构造子醒目,对使用者造成一定困难。

对于静态工厂方法的区分度不够可以通过命名规范来改进:valueOf, of, getInstance, newInstance, getType, newType

Item 2: Consider a builder when faced with many constructorparameters

场景:编写一个商品的营养成分类,有超过20种营养元素,而实际中一个商品包含的营养元素也就几种。这样一个类的构造子将会相当大,并且其中无意义的项非常多,每一个参数仅仅靠它的位置来确定它的意义,这样意义不明确,阅读起来不易懂,编写困难,容易出现错误(比如几个参数的位置弄错),这样的错误通常隐晦而难以发现(让程序及早知道错误并提示这个错误是重要的!)。比如:

NutritionFacts cocaCola =new NutritionFacts(240, 8, 100, 0, 0, 0, 35, 27, 0, 0, 0);//很难知道哪个数字对应哪个营养

一种解决方案是使用JavaBean,如下:NutritionFacts cocaCola = new NutritionFacts(); cocaCola.setServingSize(240); cocaCola.setServings(8); cocaCola.setCalories(100); cocaCola.setSodium(35); cocaCola.setCarbohydrate(27);,至少这样做参数的意义明确得多,但是这样做有严重的缺点:a JavaBean may be in aninconsistent state partway through its construction,一个构造过程被分成若干步,如果其中某一步出错,程序将会使用一个不确定的对象,这会导致很多隐晦而难以发觉的错误。还有一个缺点是:the JavaBeans pattern precludesthe possibility of making a class immutable。

使用Builder模式的解决方案代码如下: // Builder Pattern 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 { // 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 carbohydrate = 0; private int sodium = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; } } 在使用时,这样写:NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();

The Builder pattern simulates named optional parameters as found in Ada and Python.

The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters

3.Item 3: Enforce the singleton property with a private constructor or an enum type

单例模式的实现方式:

1.// Singleton with public final field public class Elvis { public static final Elvis INSTANCE = new Elvis(); private Elvis() { ... } public void leaveTheBuilding() { ... } } 这样做可以明确的知道这个对象是单例的,但是Joshua Bloch说:a privileged client can invoke the private constructor reflectively(Item 53) with the aid of the AccessibleObject.setAccessible method. If you need to defend against this attack, modify the constructor to make it throw an exception if it’s asked to create a second instance.

2.//Singleton with static factory public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { ... } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { ... } } 这样做可以让你的代码灵活可变,如果想让它不是单例的,修改getInstance方法即可,而Client代码完全无需修改。

但是,以上这两种方式在需要实现serializable接口时都会遇到问题。

3.最好的单例实现方式使用enum// Enum singleton - the preferred approach public enum Elvis { INSTANCE; public void leaveTheBuilding() { ... } } This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值