effective java在第二章Creating and Destroying Objects
中提到用 静态工厂方法的三个好处:
1.方法名的可读性
2.可以instance-controlled
实现singleton,flyweight模式
实现noinstantiableclass
实现immutable class
我想之所以静态工厂方法能够可以很好的instance-controlled,是因为讲创建的逻辑过程分离到了工厂方法里,能够很好的去controll
3.基于接口编程
jdk中的例子:
Boolean.valueof(),很好的一个flyweight和静态工厂方法的例子
java.util.Collections中就更加典型了,没有public的construtor,都是一些静态方法。特点如下:
noninstantiable
static factory methods
based interface programming along with static inner class。
这一章里引起了我对singleton pattern and flyweight pattern的区别理解