When would you use a class with static members and when would you use a Singleton class?
什么时候使用带有静态成员的类,什么时候使用单例?
Registers a class definition with this container and marks the class as a singleton class.
用这个容器注册一个类定义,并将该类定义为一个单独的类。
When copies of the singleton class run in multiple VMs, an instance is created for each machine.
当单例类的副本运行于多个虚拟机中时,每个虚拟机都会为其创建一个实例对象。
MockupDB, illustrated in Listing 3, is singleton class that simulates a database of customer orders.
清单3所示的MockupDB是个单体类,模拟客户订单的数据库。
When a singleton class is garbage-collected and then reloaded, a new singleton instance is created.
当一个单例对象被当着垃圾回收,然后该单例类又被重新加载,一个新的单例对象被创建。
BookServiceImpl is a singleton class that USES an in-memory map to maintain the collection of book objects.
BookService Impl是一个singleton类,使用一个内存映射图(in - memory map)来维护图书对象集合。
The Application class is a singleton class that acts as a controller for all of the application's operations.
Application类是一个单身类(singleton class),用作应用程序的所有操作的控制器。
A singleton class means that it is designed to have only one instance throughout the lifetime of the program.
一个单独的类,它的设计了程序的整个一生只有一个实例。
Those who use it will find that they often have to write the same code each time they want to create a different Singleton class.
他们会发现每次他们要创建一个不同的单例类的时候,都不得不写同样得代码。
The GameService class, in Listing 10, is a singleton class that represents the game server and the current state of the given game.
清单10中的gameservice类是一个单例类,代表给定游戏的游戏服务器和当前状态。
The component consists of a singleton class named PoIdGenerator, exposing a simple API, and responsible for managing blocks of POIDs.
这个组件由名为 PoIdGenerator 的单一类组成,它公开一个简单的API,并且负责管理POID 块。
These requests for a token are serviced by a singleton class named Dashboard, which manages the status of the endpoints of one or several groups.
获取令牌的请求由一个名为Dashboard的单态类进行处理,该类负责管理一个或多个组的端点的状态。
In the constructor of StockQuoteGenerator class, which is a singleton class, we are starting a new thread and assigning a Runnable object to that thread.
在stockquotegenerator类(一个单态类)的构造函数中,我们启动了一个新线程,并向该线程分配了一个Runnable对象。
Instead you can define some as an object (instead of a class) and it becomes a singleton instance of the class.
相反您可以定义一个对象(而不是类)并且它将成为类的一个单例实例。
A simple optimization to this code would be to apply the Singleton design pattern to class Empty, because every instance of Empty is identical.
这段代码的一个简单优化是对empty类应用同一个设计类型,因为每一个Empty的实例都是同样的。
By using a ThreadLocal in our Singleton, as shown in Listing 3, we can allow any class in our program to easily acquire a reference to a per-thread Connection.
如清单3所示,通过使用“单子”中的ThreadLocal,我们就能让我们的程序中的任何类容易地获取每线程Connection的一个引用。
As its name suggests, the Singleton Pattern is typically used when you want only one instance of a particular class.
如其名称所暗示的,当您想要特别类的仅仅一个实例时,才使用Singleton模式。
Most caches are implemented using the Singleton pattern involving a static reference to a top level cache class as shown in this example.
使用Singleton模式实现的大多数缓存都涉及本例所示的对顶级cache类的静态引用。
Users of the class can then add and remove Attendee elements from the collection the Singleton instance holds, or replace the elements in the collection.
然后,该类的用户可以在Singleton实例保存的集合中添加和删除Attendee元素,或替换集合中的元素。
The Message singleton serves as a metadata repository and factory class for the Message class.
单实例对象Message作为message类的元存储库和工厂类。
MembersAppDomainPMF (found under AcmeMemberAppWeb > src > org.acme.membership.jdo) is the class where we have placed our singleton PersistenceManagerFactory attribute.
MembersAppDomainPMF(可以在AcmeMemberAppWeb >src > org.acme.membership.jdo 下面找到)就是我们放置框架PersistenceManagerFactory属性位置的类。
This class is a singleton, meaning there's only one map of running server instances for all projects.
这是一个惟一的类,也就意味着所有项目正在运行的服务器实例只有一个惟一的映射。
The only other method (set in italics) in the class is used to get a reference to the singleton and to create it if it is not yet instantiated.
该类中仅剩的另一个方法(用斜体标识)用于获取该单件的一个引用,如果尚未实例化则创建它。
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.
单例模式是一种非常有用的设计模式,它允许你的类对外只提供一个实例,但是常犯的错误在于不止一个实例被创建。
This has the added benefit of separating singleton code from the class code leaving the flexibility to use several instances of the class or using the class as a singleton.
这样分离出单例结构的代码,将有利于保持按单例模式使用类或不按单例模式使用类的灵活性。
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.
这是一个可用来创建单例类实例确不需要为每个特定的类重写单例模式代码的可重用的类。
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.
比如数据库连接池就是单例设计模式的一个例子:我们一般不想让应用程序具有连接池类的多个资源密集型实例。
With singletons implemented as single instances instead of static class members, you can initialize the singleton lazily, creating it only when it is first used.
由于单例对象作为实例存在而不是类的静态成员,所以你能够惰性地初始化单例对象,即只需要在第一次用它的时候才去创建它。
The singleton pattern ensures that only a single instance of a class is normally used by an application, but still allows alternate instances to be created as required.
单元素模式可以确保应用程序通常只使用类的单个实例,但是仍然允许根据需要创建备用实例。
Listing 4 shows a version of the singleton that USES a method instead of an attribute to specify the appropriate class.
清单4显示了使用方法而非属性来指定适当类的单例。
应用推荐