Objective-C singleton)

Source: Internet
Author: User

If you want to write a class to ensure that only one instance exists and you can get the entry for providing services for this specific instance, you can use the single-State design mode.
The single-State mode is commonly used in Java and C ++. It can also be implemented in Cocoa.
However,
The Objective-C Singleton mode is definitely different from what you think. Its writing method is different from the writing method in all languages you have ever seen.
 
Official recommendations
 
Because it is risky to design the single-State mode, it is mainly because of problems that may occur in the case of multithreading. Therefore, Apple officially recommends using the following methods to implement the single-State mode:
Static MyGizmoClass * sharedGizmoManager = nil;
+ (MyGizmoClass *) sharedManager
{
@ Synchronized (self ){
If (sharedGizmoManager = nil ){
[[Self alloc] init]; // assignment not done here
}
}
Return sharedGizmoManager;
}
+ (Id) allocWithZone :( NSZone *) zone
{
@ Synchronized (self ){
If (sharedGizmoManager = nil ){
SharedGizmoManager = [super allocWithZone: zone];
Return sharedGizmoManager; // 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;
}
 
Open-source template (see Attachment download)
Programmers are all lazy. Nowadays, it is popular to use a macro definition to deal with these many things, and they are more considerate.
The Singleton includes the following APIs:

+ (MyClass *) sharedInstance;
+ (Void) purgeSharedInstance;
 
When sharedInstance is called, a single instance is created and returned.
Calling purgeSharedInstance will destroy the singleton
Manually calling alloc can also be a singleton. You can call
[[MyClass alloc] initWithParam: firstParam secondParam: secondParam];
You only need to ensure that it is called before sharedInstance, because there is only one creation opportunity.
The following is a macro statement:

MyClass. h:
==========================================================
# Import "SynthesizeSingleton. h"

@ Interface MyClass: SomeSuperclass
{
...
}
SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER (MyClass );

@ End
==========================================================


MyClass. m:
==========================================================
# Import "MyClass. h"

@ Implementation MyClass

SYNTHESIZE_SINGLETON_FOR_CLASS (MyClass );

...

@ End
==========================================================
This article is from the "arsurchen" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.