In a previous article on the object of the copy of the knowledge, today we look at the singleton mode in OC, the singleton mode in the design pattern is probably the most one, but also the simplest one
There are three conditions for implementing a singleton mode
1, the construction method of the class is private
2. class provides a class method for generating an object
3. There is a private object in the class
For these three conditions, OC can be done
1, the construction method of the class is private
We just need to rewrite the Allocwithzone method to have the initialization run only once
2. class provides a class method to produce an object
This can directly define a class method
3. There is a private object in the class
We can define a property in the. m file to
Here's a look at the code:
AdressBook.h
1 // 2 //AdressBook.h3 //35_singleton4 // 5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei. All rights reserved. 7 // 8 9 #import<Foundation/Foundation.h>Ten One //Design The purpose of the simple interest class to restrict the class from creating only one object A - //The construction method is private. - //to save a global static variable the @interfaceAdressbook:nsobject - -+ (Adressbook *) shareinstance; - + @end
A class method is provided in the. h file to produce the object's
Adressbook.m
1 // 2 //ADRESSBOOK.M3 //35_singleton4 // 5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei. All rights reserved. 7 // 8 9 #import "AdressBook.h" Ten One StaticAdressbook *instance = nil;//cannot allow external access, while placed in a static block A - @implementationAdressbook - the+ (Adressbook *) shareinstance{ - if(Instance = =Nil) { -Instance =[[Adressbook alloc] init]; - } + returninstance; - } + A //restriction methods, classes can only be initialized once at //Alloc is called when -+ (ID) Allocwithzone: (struct_nszone *) zone{ - if(Instance = =Nil) { -Instance =[Super Allocwithzone:zone]; - } - returninstance; in } - to //Copy Method +- (ID) Copywithzone: (Nszone *) zone{ - returninstance; the } * $ //you need to rewrite the release method, and you can't refer to +1Panax Notoginseng- (ID) retain{ - returnSelf ; the } + A //you need to rewrite the release method and not have it reference-1 the-(OneWayvoid) release{ + //Do nothing ... - } $ $- (ID) autorelease{ - returnSelf ; - } the - @end
We have defined a static type of object
1 Static Adressbook *instance = nil; // cannot allow external access, while placed in a static block
Provides a class method to get a single instance
1 + (Adressbook *) shareinstance{ 2 if(instance = = Nil) { 3 instance = [[Adressbook alloc] init]; 4 } 5 return instance; 6 }
Override the Allocwithzone method so that initialization is done only once
1 // 2 // alloc call 3 + (id ) Allocwithzone: (struct _nszone * Span style= "color: #008080;" >4 if (instance == nil) { 5 instance = [Super Allocwithzone:zone]; 6 } return instance; 8 }
There are other ways to rewrite it.
1 //Copy Method2- (ID) Copywithzone: (Nszone *) zone{3 returninstance; 4 } 5 6 //you need to rewrite the release method, and you can't refer to +17- (ID) retain{8 returnSelf ; 9 } Ten One //you need to rewrite the release method and not have it reference-1 A-(OneWayvoid) release{ - //Do nothing ... - } the -- (ID) autorelease{ - returnSelf ; -}
The Copy method can only return the current single instance
The retain and release methods cannot be referenced by the +1 and-1 operations
Test code
Main.m
1 // 2 //main.m3 //35_singleton4 // 5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei. All rights reserved. 7 // 8 9 #import<Foundation/Foundation.h>Ten One #import "AdressBook.h" A - //Simple Interest Mode - intMainintargcConstCharchar *argv[]) { the @autoreleasepool { - -Adressbook *book1 =[Adressbook shareinstance]; -Adressbook *book2 =[Adressbook shareinstance]; + -NSLog (@"%@", Book1); +NSLog (@"%@", BOOK2); A at } - return 0; -}
Two pointers point to the same object
Summarize
This article mainly introduces the singleton mode in OC, and the series of articles of our OC Learning article is finished, of course, these do not represent all the contents of OC, there are many other content, we only have to slowly use the back to learn more.
(reprint) OC Study---Single-case model