IOS 设计模式之单例模式

要实现一个Singleton Class, 至少需要做以下四个步骤:
1. 为Singleton Object实现一个静态实例, 初始化, 然后设置成nil.
2. 实现一个实例构造方法(通常命名为 sharedInstance 或者 sharedManager)检查上面声名的静态实例是否为nil, 如果是则新建并返回一个本类实例.
3. 重写 allocWithZone: 方法来保证当其他人直接使用 alloc 和 init 试图获得一个新实例的时候不会产生一个新的实例.
4. 适当的实现 copyWithZone:, release, retain, retainCount 和 autorelease.

static
ZDMyInfo *_sharedMyInfo = nil; +(ZDMyInfo *)sharedInstance { if (_sharedMyInfo == nil) { _sharedMyInfo = [[ZDMyInfo alloc] init]; } return _sharedMyInfo; }
原文地址:https://www.cnblogs.com/joesen/p/3079752.html