因为根据设计它是线程安全的,创建它之后可以把它缓存在一个公共静态最终变量中,或者包装在单实例模式中以供以后访问。
Since it is thread safe by design, you might cache it in a public static final variable, or wrap it in a singleton pattern after creation for later access.
单例模式可能是最常用的OOP设计模式之一了;它限制了一个类的对象实例数只能为1。
The singleton pattern is probably one of the most often used OOP design patterns; it restricts the number of object instances of a class to one.
单例模式是一种非常有用的设计模式,它允许你的类对外只提供一个实例,但是常犯的错误在于不止一个实例被创建。
The singleton is a useful design pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created.
单例模式是一种有用的控制类访问的途径,他能保证只有一个类实例存在。
Singletons are a useful way to control access to a class, making sure that only one instance can exist.
比如数据库连接池就是单例设计模式的一个例子:我们一般不想让应用程序具有连接池类的多个资源密集型实例。
An example use case for a singleton would be a database connection pool: you don't want your application to have multiple resource-intensive instances of a connection pool class.
这是一个可用来创建单例类实例确不需要为每个特定的类重写单例模式代码的可重用的类。
This is a class that can be used repeatedly to instantiate a class as a singleton without having to re-write the singleton pattern code for that specific class.
单例模式的目的在于控制对象的创建,限制对外提供的对象数量为1,同时允许在情况变化时创建更多的实例。
The singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.
相反,Scala支持单例模式,可以应用于那些一种类型只有一个实例的情况。
Instead, Scala supports a singleton object construct to support those cases where exactly one instance of a type is needed.
同时,如果使用创建单实例的方法实现单例模式,单例类还具有创建多个实例的变通能力。
Likewise, with a singleton implemented as single instance, you leave open the possibility of altering the class to create more instances in the future.
单例模式:确保一个类只有一个实例,并提供一个全局访问点。
The Singleton Pattern:ensures a class has only on instance, and provides a global point of access to it.
使用类来实现单例模式可以确保在任何时间只有一个实例对象。
Classes implementing Singleton pattern will ensure that only one instance of the object ever exists at any one time.
单例模式-每个依赖服务的组件,都是获得服务工厂生成单个实例的引用。服务是一个单例对象或函数,对外提供特定的功能。
Singletons - Each component dependent on a service gets a reference to the single instance generated by the service factory.
单例模式-每个依赖服务的组件,都是获得服务工厂生成单个实例的引用。服务是一个单例对象或函数,对外提供特定的功能。
Singletons - Each component dependent on a service gets a reference to the single instance generated by the service factory.
应用推荐