简体   繁体   English

有效java中的静态工厂方法

[英]Static factory methods in effective java

In Effective Java , Item 1, it says that the static factory methods made the Collections framework much smaller than it would have been. 在Effective Java,第1项中,它说静态工厂方法使Collections框架比原本要小得多。 Could someone please explain how ? 有人可以解释一下吗? I can't understand how the following is possible just because of using static factory methods ? 由于使用静态工厂方法,我无法理解以下是如何实现的? I mean we still have to implement those separate implementations don't we ? 我的意思是我们仍然必须实现那些单独的实现不是吗?

The Collections Framework API is much smaller than it would have been had it exported thirty-two separate public classes, one for each convenience implementation. Collections Framework API比它导出32个单独的公共类要小得多,每个方便实现一个。

By "smaller" they mean "less classes". “较小”意味着“较少的阶级”。

Instead of providing lots of classes for each variation of implementation, instead factory methods have been provided that return such implementations without the need to have their classes declared as top-level classes (less "class bloat"). 而不是为每个实现变体提供许多类,而是提供了返回这样的实现的工厂方法,而不需要将它们的类声明为顶级类(更少“类膨胀”)。

I think the meaning here is that there's only core implementations in java collections API, if you need synchronisation, etc "wrappers" are provided by static factories. 我认为这里的含义是java集合API中只有核心实现,如果需要同步,等等“静态工厂提供”包装器“。

So there's 8 implementations and without "wrappers" there would be more (size gain). 所以有8个实现,没有“包装”会有更多(大小增益)。

See http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html for more details 有关详细信息,请参阅http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM