简体   繁体   English

单例和静态工厂方法有什么区别

[英]What is the difference between a Singleton and static factory methods

I want to know if both singleton and static factory method create only one instance then why there are two concepts for same purpose?我想知道如果单例和静态工厂方法都只创建一个实例,那么为什么有两个概念用于同一目的?

Note: Here term "static factory method" is taken from Effective java book by Joshua bloch where he has written:注意:这里的术语“静态工厂方法”取自 Joshua bloch 所写的 Effective java 书籍:

"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. This allows immutable classes (Item 15) to use preconstructed instances, or to cache instances as they're constructed, and dispense them repeatedly to avoid creating unnecessary duplicate objects. The Boolean.valueOf(boolean) method illus- trates this technique: it never creates an object. This technique is similar to the Flyweight pattern [Gamma95, p. 195]. It can greatly improve performance if equivalent objects are requested often, especially if they are expensive to create. The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) “静态工厂方法的第二个优点是,与构造函数不同,它们不需要在每次调用时都创建一个新对象。这允许不可变类(第 15 条)使用预先构造的实例,或缓存实例,因为它们'重新构造,并重复分配它们以避免创建不必要的重复对象。Boolean.valueOf(boolean) 方法说明了这种技术:它从不创建对象。这种技术类似于享元模式 [Gamma95, p. 195]。如果经常请求等效对象,它可以极大地提高性能,特别是如果创建它们的成本很高。静态工厂方法从重复调用中返回相同对象的能力允许类在任何时候保持对哪些实例存在的严格控制。这样做据说是实例控制的。写实例控制的类有几个原因。实例控制允许一个类保证它是单例(条目3) or noninstantiable (Item 4) "或不可实例化(第 4 项)

The point being made by the line " they are not required to create a new object each time they're invoked " is that (unlike new , which always creates a new object) a factory method could be implemented in some more clever way that reuses an existing object. 他们不需要在每次调用时都创建一个新对象”这一行提出的观点是(与new不同,它总是创建一个新对象)可以以更聪明的方式实现工厂方法,重用一个现有的对象。

Take a look at the of() factory method in Guava's ImmutableList .看看 Guava 的ImmutableList中的of()工厂方法。 This method returns "the" empty immutable list - there's no point constructing a new object every time of() is called, instead the same object is always returned.这个方法返回“那个”空的不可变列表——每次调用of()时构造一个新对象是没有意义的,而是总是返回相同的对象。 This is safe because any empty ImmutableList instance is functionally indistinguishable from any other empty instance.这是安全的,因为任何空的ImmutableList实例在功能上都与任何其他空实例没有区别。

A static factory method is a means of construction, it may return new instances, alternate subclasses of a type, wrap critical logging or registry, compose a number of items into an object, or (maybe) return back a single static instance.静态工厂方法是一种构造方法,它可以返回新实例、类型的替代子类、包装关键日志记录或注册表、将多个项目组合到一个对象中,或者(可能)返回单个静态实例。

A singleton obtained by any means always resolves back to the same instance.通过任何方式获得的单例总是解析回同一个实例。 This means there is no variability.这意味着没有可变性。

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

相关问题 all-static-methods和应用单例模式有什么区别? - What is the difference between all-static-methods and applying a singleton pattern? 使用 singleton class 和 class 与 ZA81259CEF8E959C624DF1D456E5D2 方法有什么区别? - What is difference between using singleton class and class with static methods? 静态接口方法与普通单例工厂之间的区别? - Difference between static Interface method generic singleton factory? "什么是静态工厂方法?" - What are static factory methods? singleton 在依赖注入框架(如 Dagger)上的实现与正常的 static singleton 有什么区别? - What is the difference between the implementation of singleton on Dependency Injection frameworks (like Dagger) than the normal static singleton? Java接口中的静态方法和默认方法有什么区别? - What is the difference between static and default methods in a Java interface? Static方法和Instance方法的区别 - Difference between Static methods and Instance methods 静态和非静态方法之间的区别 - difference between static and non-static methods 具有同步功能的单例类和静态同步功能之间有什么区别 - What's the difference between a singleton Class with synchronized function and a static synchronized function 单例类中的私有静态变量和私有实例变量有什么区别? - What is the difference between private static variable and private instance variable in a singleton class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM