Singleton 模式主要用来提供全局共享对象,在Objective-C中通过定义静态全局变量可实现这一模式:
//manager.h @interface Manager:NSObject + (id)instance; @end //manager.m #import "manager.h" static Manager *sharedManager = nil; @implementation Manager + (id)instance { if(sharedManager == nil) { sharedManager = [[super alloc]init]; } return sharedManager; } @end
使用时,直接调用[[Manager intance] dosomething];