A singleton class returns the same instance no matter how many times an application requests it. A typical class permits callers to create as many instances of the class as they want, whereas with a singleton class, there can be only one instance of the class per process. A singleton object provides a global point of access to the resources of its class. Singletons are used in situations where this single point of control is desirable, such as with classes that offer some general service or resource.
无论你在应用程序请求多少次,单例类都返回相同的实例。一个典型类允许调用者可根据需要创建尽可能多的类的实例,而对于单例类来说,每个进程中只能有一个类的实例。单粒对象为为类资源停工全局的访问入口,在何时的情况下你可以选择使用单粒,比如能提供通用的服务或资源的类
You obtain the global instance from a singleton class through a factory method. The class lazily creates its sole instance the first time it is requested and thereafter ensures that no other instance can be created. A singleton class also prevents callers from copying, retaining, or releasing the instance. You may create your own singleton classes if you find the need for them. For example, if you have a class that provides sounds to other objects in an application, you might make it a singleton.
通过工厂从单例类中获取全局实例,类在第一次请求的时候通过懒加载的方式创建唯一的实例,此后确保不会创建其他实例。单例类避免调用者复制、保留、释放实例。当你需要的时候可以自定义单例类。比如
Several Cocoa framework classes are singletons. They include NSFileManager, NSWorkspace, and, in UIKit, UIApplication and UIAccelerometer. The name of the factory method returning the singleton instance has, by convention, the form sharedClassType. Examples from the Cocoa frameworks are sharedFileManager, sharedColorPanel, and sharedWorkspace.
某些Cocoa框架类是单例:NSFileManager、NSWorkspace。UIKit中:UIApplication、UIAccelerometer。返回单例的工厂方法名是sharedClassType,Cocoa frameworks中的例子有:sharedFileManager, sharedColorPanel, and sharedWorkspace.
【总结】
1.某个类只能有一个实例
2.某个类需要全局提供自己的实例