如何在我的iPhone项目中实现Singleton模式?
我正在一个项目中,我想要使用Singleton模式模式。 我想要我的这个项目的任何数据模型休闲Singleton模式。 我研究有关这个苹果文件
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6
和
http://www.oodesign.com/singleton-pattern.html
http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/
现在我知道我的自定义对象类应该休闲分配一个对象的主要规则,但我需要完整的实现像使用这个类的对象我是新的iPhone应用程序开发,所以如果我在这个问题的任何地方错了,请指导
static MyClass *_sharedInstance; + (MyClass *)sharedMyClass { @synchronized([MyClass class]) { if (_sharedInstance == nil) [[self alloc] init]; return _sharedInstance; } return nil; } +(id) alloc { @synchronized([MyClass class]) { NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of MyClass."); _sharedInstance = [super alloc]; return _sharedInstance; } return nil; } + (id) allocWithZone:(NSZone *)zone { @synchronized([MyClass class]) { NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of MyClass."); _sharedInstance= [super allocWithZone:zone]; return _sharedInstance; } return nil; //on subsequent allocation attempts return nil } - (id) copyWithZone:(NSZone *)zone { return self; } - (id)retain { return self; } - (NSUInteger)retainCount { return NSUIntegerMax; } - (oneway void)release { // Do nothing } - (id)autorelease { return self; }
尝试这个:
@implementation Singleton + (Singleton *)sharedInstance { static Singleton *obj = nil; if (obj == nil) obj = [[self alloc] init]; return obj; } @end
如果你可以瞄准iOS 4或以上,我会采取以下的方式:
//.h +(MySingletonClass *)mySharedInstance; -(void)doSomething; //.m +(MySingletonClass *)mySharedInstance { static dispatch_once_t pred; static MySingletonClass *shared = nil; dispatch_once(&pred, ^{ shared = [[MySingletonClass alloc] init]; }); return shared; } -(void)doSomething { } // override also the init if you want
要访问它,请执行#import MySingletonClass.h
,并在任何需要的地方使用它,如下所示:
MySingletonClass* mySharedInstance = [MySingletonClass mySharedInstance]; [mySharedInstance doSomething];
我想要我的这个项目的任何数据模型休闲Singleton模式。
根据我的经验,我不会滥用单身。 应用程序可能变得难以维护。 为了避免这种情况,把数据模型放在你的单例中。 您可以直接访问数据模型(创build属性)或使用公共方法(如doSomething
)作为包装器。
希望这可以帮助。
这可能是一个有用的参考: http : //cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html
通常,我在中创build对象
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法,并让它住在那个对象中。 我用一个macros把它提供给应用程序的其余部分:
#define APPLICATION ((AppDelegate*)([UIApplication sharedApplication].delegate))
作为应用程序代理的只读属性
这种types的方法,如果你在嘲笑的好处是,它只是另一个对象的属性,而不是一个隐藏的静态对象。
我用:
#import <Foundation/Foundation.h> @interface iCode_Framework : NSObject @property (readonly, nonatomic) unsigned int iBufCapacity; @property (readonly, nonatomic) unsigned int iPort; @property (readonly, nonatomic) NSString * urlStr; @end #import "iCode_Framework.h" static iCode_Framework * instance; @implementation iCode_Framework @dynamic iBufCapacity; @dynamic iPort; @dynamic urlStr; - (unsigned int)iBufCapacity { return 1024u; }; - (unsigned int)iPort { return 1978u; }; - (NSString *)urlStr { return @"localhost"; }; + (void)initialize { if (!instance) { instance = [[super allocWithZone:NULL] init]; } } + (id)allocWithZone:(NSZone * const)notUsed { return instance; } @end
这和正常的类一样使用,你调用alloc和init! 将variables分配给一个简写通常是很方便的,因为alloc和init很长,例如:
#import "iCode_FrameworkTests.h" #import "iCode_Framework.h" static iCode_Framework * c; @implementation iCode_FrameworkTests + (void)initialize { c = [[iCode_Framework alloc] init]; } - (void)setUp { [super setUp]; // Set-up code here. } - (void)tearDown { // Tear-down code here. [super tearDown]; } - (void)testSingletonNotNil { STAssertNotNil(c, nil); } - (void)testSingletonProperty { STAssertEqualObjects(c, [iCode_Framework alloc], nil); } - (void)testIBufCapacity { STAssertEquals(c.iBufCapacity, 1024u, nil); } @end
这种方法的优点是它和任何其他类一样使用,因此可以被模拟进行testing。