单例模式-Sington

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.某个类需要全局提供自己的实例

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值