Effective Java边看边翻-Item 一: Consider static factory methods instead of cons
Effective Java边看边翻-Item 1: Consider static factory methods instead of cons
原文要点翻译:
用static factory methods替换public constrctor method的优点:
1.One advantage of static factory methods is that, unlike constructors, they have names.
1.第一个好处是,不同于构造方法,他们有自己独特的名字。
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.
2.第二个好处是,不同于构造方法,他们不必在每次被调用时生成一个新的实例(对象)。
3.A third advantage of static factory methods is that, unlike constructors,
they can return an object of any subtype of their return type.
3.第三个好处是,不同于构造方法,他们可以返回一个返回类型的子类。
4.A fourth advantage of static factory methods is that they reduce the verbosity
of creating parameterized type instances.
4.第四个好处是,可以避免在生成具有参数类型的实例(博主:比如HashMap<String, Object>)时候的冗余代码。
用static factory methods替换public constrctor method的缺点:
1.The main disadvantage of providing only static factory methods is that
classes without public or protected constructors cannot be subclassed.
1.只提供共有静态方法生成实例的类型最大的缺点是,
没有public或protected构造方法的类型不能被子类化。
2.A second disadvantage of static factory methods is that they are not
readily distinguishable from other static methods.
2.另一个静态工厂方法的缺点是,他们不会很快的与其他静态方法区分开来。
In summary, static factory methods and public constructors both have their
uses, and it pays to understand their relative merits. Often static factories are preferable,
so avoid the reflex to provide public constructors without first considering
static factories.
总结起来说,静态工厂方法以及共有构造方法都有他们自己的用处,
而且了解他们各自的优点是很值得的。
大多数情况下静态工厂是受欢迎的,所以请避免还没有考虑过静态工厂方法就采用共有构造方法的先入为主的观念。
个人理解:
如果每次使用一个类的时候,如好处2,并不需要每次都生成一个新的实例(instance)的时候是很适用的(比如单例)。
另外,好处3里面说的,可以隐藏具体实现(implementation)的作用,使系统变得更加灵活(比如要更换或增加一种实现的时候)。
(未完。。。)
原文要点翻译:
用static factory methods替换public constrctor method的优点:
1.One advantage of static factory methods is that, unlike constructors, they have names.
1.第一个好处是,不同于构造方法,他们有自己独特的名字。
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.
2.第二个好处是,不同于构造方法,他们不必在每次被调用时生成一个新的实例(对象)。
3.A third advantage of static factory methods is that, unlike constructors,
they can return an object of any subtype of their return type.
3.第三个好处是,不同于构造方法,他们可以返回一个返回类型的子类。
4.A fourth advantage of static factory methods is that they reduce the verbosity
of creating parameterized type instances.
4.第四个好处是,可以避免在生成具有参数类型的实例(博主:比如HashMap<String, Object>)时候的冗余代码。
用static factory methods替换public constrctor method的缺点:
1.The main disadvantage of providing only static factory methods is that
classes without public or protected constructors cannot be subclassed.
1.只提供共有静态方法生成实例的类型最大的缺点是,
没有public或protected构造方法的类型不能被子类化。
2.A second disadvantage of static factory methods is that they are not
readily distinguishable from other static methods.
2.另一个静态工厂方法的缺点是,他们不会很快的与其他静态方法区分开来。
In summary, static factory methods and public constructors both have their
uses, and it pays to understand their relative merits. Often static factories are preferable,
so avoid the reflex to provide public constructors without first considering
static factories.
总结起来说,静态工厂方法以及共有构造方法都有他们自己的用处,
而且了解他们各自的优点是很值得的。
大多数情况下静态工厂是受欢迎的,所以请避免还没有考虑过静态工厂方法就采用共有构造方法的先入为主的观念。
个人理解:
如果每次使用一个类的时候,如好处2,并不需要每次都生成一个新的实例(instance)的时候是很适用的(比如单例)。
另外,好处3里面说的,可以隐藏具体实现(implementation)的作用,使系统变得更加灵活(比如要更换或增加一种实现的时候)。
(未完。。。)