Most
Because in iOS app development, considering the encapsulation of some public methods, we decided to use the notation of the singleton pattern. I wonder if the singleton pattern in Object-c is different from that in Java?
So Antioch from the Internet a search, found that " objective-c singleton mode " a article by many people
Reproduced, the main contents are as follows
Apple's official recommendations
Since there is a certain risk in designing a single mode, the main consideration is the problem that may occur in multi-threaded situations, so Apple recommends using the following methods to achieve a single mode:
static Mygizmoclass *sharedgizmomanager = nil;
+ (mygizmoclass*) Sharedmanager
{
@synchronized (self) {
if (Sharedgizmomanager = = nil) {
[[Self alloc] init]; Assignment not do 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, cannot be released
}
-(void) Release
{
Do nothing
}
-(ID) autorelease
{
return self;
}
According to my experience in Java development, I will generally use the following wording
static Mygizmoclass *sharedgizmomanager = nil;
+ (mygizmoclass*) Sharedmanager
{
@synchronized (self) {
if (Sharedgizmomanager = = nil) {
[[Self alloc] init]; Assignment not do 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
}
Change into
static Mygizmoclass *sharedgizmomanager = nil;
+ (mygizmoclass*) Sharedmanager
{
@synchronized (self) {
if (Sharedgizmomanager = = nil) {
Sharedgizmomanager = [[Self alloc] init]; Assignment not done
Here
}
}
return sharedgizmomanager;
}
I don't know what else to do here. (ID) Allocwithzone: (Nszone *) zone?
But on second thought, since Apple's official proposal to do so, it certainly has its role, so Antioch decided to write a test demo, a proven fine!
Test class
Call
Console output Results
At this point finally the truth. original [[Self alloc] init]; Called when the + is called by default
(ID) Allocwithzone: (Nszone
*) Zone Method: Sharedgizmomanager is finally in **allocwithzone: (Nszone
) The zone method completes the initialization operation: *
Sharedgizmomanager = [Super Allocwithzone:zone];
Antioch further check cocoa document, original allocwithzone: (Nszone
*) zone is given to the object **sharedgizmomanager
Allocation of memory space. Its * * **zone** can be imagined as a memory pool, alloc,allocwithzone or dealloc these operations, all in this memory pool operation. Cocoa always configures a default Nszone, and any default memory operation is done on this "zone". The drawback of the default nszone is that it is global in scope, and time is long, which inevitably leads to fragmentation of memory, and if you need a lot of alloc some object, performance is affected. All cocoa provides a way for you to generate a nszone yourself (which is actually what I've done with the demo, rewrite the Allocwithzone method), and will Alloc,
Copy is restricted to this "zone".
The singleton mode of OBJECTIVE-C