}+ (MySingleton *)sharedInstance;// Interface- (NSString *)helloWorld;@end#import "MySingleton.h"static MySingleton *sharedInstance = nil;@implementation MySingleton#pragma mark Singleton methods+ (MySingleton *)sharedInstance { @synchronized(self) { if (sharedInstance == nil) { sharedInstance = [[MySingleton alloc] init]; } } return sharedInstance;}+ (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (sharedInstance == nil) { sharedInstance = [super allocWithZone:zone]; return sharedInstance; // assignment and return on first allocation } } return nil; // on subsequent allocation attempts return nil}- (id)copyWithZone:(NSZone *)zone { return self;}- (id)retain { return self;}- (unsigned)retainCount { return UINT_MAX; // denotes an object that cannot be released}- (void)release { //do nothing}- (id)autorelease { return self;}#pragma mark -#pragma mark NSObject methods- (id)init { if (self = [super init]) { //。} return self;}- (void)dealloc { //。
[super dealloc];}#pragma mark -#pragma mark Implementation- (NSString *)helloWorld { return @"Hello World!";}@end#import "MySingleton.h"//。NSLog(@"Result for singleton method helloWorld: %@", [[MySingleton sharedInstance] helloWorld]); 。
4.ios 单例设计模式 解决什么用单例模式:在单例模式中,对活动的单例只有一个实例 。
对单例类的所有实例化得到的都是相同的一个实例 。这个模式也提供一个全局的接口来访问这个类的实例 。
public class Singleton { //Fields private static Singleton instance; //Standard default Constructor protected Singleton(){}; //Static method for creating the single instance of the Constructor public static Singleton Instance(){ //initialize if not already done if(instance == null) instance = new Singleton(); //return the initialized instance of the Singleton Class return instance; } }public class Client { public static void main(String []args){ Singleton s1 = Singleton.Instance(); Singleton s2 = Singleton.Instance(); if(s1 == s2) System.out.println("The same instance"); } }单例模式的优点: 1 。实例控制:单例模式防止其它对象对自己的实例化,确保所有的对象都访问一个实例 。
2 。伸缩性:因为由类自己来控制实例化进程,类就在改变实例化进程上有相应的伸缩性 。
5.ios 程序启动时为什么重新创建单例类[cpp] view plaincopy static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } 当然,在iOS4之后有了另外一种写法:[cpp] view plaincopy+ (AccountManager *)sharedManager { static AccountManager *sharedAccountManagerInstance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ sharedAccountManagerInstance = [[self alloc] init]; }); return sharedAccountManagerInstance; } 该写法来自 objcolumnist,文中提到,该写法具有以下几个特性:1. 线程安全 。
2. 满足静态分析器的要求 。3. 兼容了ARC 然后我还有点好奇的是dispatch_once,这个函数,没见过啊 。
于是就到官方的文档里找找看,是怎么说的 。下面是官方文档介绍:dispatch_once Executes a block object once and only once for the lifetime of an application. void dispatch_once( dispatch_once_t *predicate, dispatch_block_t block); Parameters predicate A pointer to a dispatch_once_t structure that is used to test whether the block has completed or not.block The block object to execute once.Discussion This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.If called simultaneously from multiple threads, this function waits synchronously until the block has completed.The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined.Availability Available in iOS 4.0 and later.Declared In dispatch/once.h 我们看到,该方法的作用就是执行且在整个程序的声明周期中,仅执行一次某一个block对象 。
- nurse的音标怎么写
- 给餐厅的好评怎么写
- 植树节看图写话怎么写
- 二年级寒假作业怎么写
- 怎么写观察日记300字
- 90元的大写怎么写
- 玩具店铺简介怎么写
- 申请采购的邮件怎么写
- 老kuai怎么写
- 怀孕后觉得憋气是怎么回事
