http://blog.csdn.net/a451493485/article/details/8624990
C code
- @implementation Appsharedatamanager
- Static Appsharedatamanager * Sharedatamanager = nil;
- @synthesize Thecurrentlanguage;
- @synthesize Presentmodalflag;
- ..........
- + (Appsharedatamanager *) Sharedmanager
- {
- if (Sharedatamanager = = nil)
- {
- Sharedatamanager = [[Appsharedatamanager alloc] init];
- }
- return Sharedatamanager;
- }
- -(ID) init{
- self = [super init];
- if (self) {
- //Object instance initialization
- Thecurrentlanguage = [Zonuserdefaultmanager getappdefaultlanguage];
- ........
- }
- return self;
- }
Debug discovery
Appsharedatamanager *a = [[Appsharedatamanager alloc] init];
NSLog (@ "a:%@", A);
Appsharedatamanager *b = [Appsharedatamanager Sharedmanager];
NSLog (@ "b:%@", B);
Print Out is the
2013-02-28 23:27:25.368 Zon2012[10647:c07] a:<appsharedatamanager:0x12638630>
2013-02-28 23:27:25.369 Zon2012[10647:c07] b:<appsharedatamanager:0xb584b40>
Not a memory address, which is not the same entity!
This is one of Apple's official single-case recommendations:
https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/cocoafundamentals/cocoaobjects/ Cocoaobjects.html#//apple_ref/doc/uid/tp40002974-ch4-sw32
Roughly as follows:
Java code
- /* Singleton.h */
- #Import < Foundation/foundation.h>
- @interface Singleton:nsobject
- + (Singleton *) instance;
- @end
- /* SINGLETON.M */
- #Import "Singleton.h"
- static Singleton *instance = nil;
- @implementation Singleton
- + (Singleton *) Instance {
- if (!instance) {
- instance = [[super Allocwithzone:null] init];
- }
- return instance;
- }
- + (ID) Allocwithzone: (Nszone *) Zone {
- return [self instance];
- }
- -(ID) Copywithzone: (Nszone *) Zone {
- return self;
- }
- -(ID) init {
- if (instance) {
- return instance;
- }
- self = [super init];
- return self;
- }
- -(ID) retain {
- return self;
- }
- -(oneway void) Release {
- // do nothing
- }
- -(ID) autorelease {
- return self;
- }
- -(Nsuinteger) Retaincount {
- return Nsuintegermax;
- }
- @end
This is a very standard singleton implementation, good. However, this implementation is not thread-safe. So the great gods of each way show their divinity, and give the realization of a variety of single-case patterns.
Now put him under the step:
Java code
- Static MyClass *class = nil;
- @implementation MyClass
- + (MyClass *) sharedmyclass{
- @synchronized (self) { //To ensure the uniqueness of the entity in the case of multithreading
- if (! Class) {
- [[Self alloc] init]; //This method calls Allocwithzone
- }
- }
- return class;
- }
- + (ID) Allocwithzone: (Nszone *) zone{
- @synchronized (self) {//////To ensure the uniqueness of the entity in the case of multithreading
- if (! Class) {
- class = [Super Allocwithzone:zone]; //Make sure to use the same memory address
- return class;
- }
- }
- return nil;
- }
- -(ID) init
- {
- if (Class) {
- return class;
- }
- if (self = [Super init]) {
- //Perform some initialization
- }
- return self;
- }
- -(ID) Copywithzone: (Nszone *) zone; {
- return self; //Ensure that the Copy object is also unique
- }
- -(ID) retain{
- return self; //Ensure that the count is unique
- }
- -(unsigned) retaincount
- {
- return Uint_max; //loaded, so that the printed count is always-1
- }
- -(ID) autorelease
- {
- return self; //Ensure that the count is unique
- }
- -(oneway void) Release
- {
- //Override Count Release method do nothing
- }
- @end
Re-commissioning
MyClass *a = [[MyClass alloc] init];
NSLog (@ "a:%@", A);
MyClass *b = [MyClass sharedmyclass];
NSLog (@ "b:%@", B);
MyClass *c = [A copy];
NSLog (@ "c:%@", C);
Print Out is the
A:<myclass:0x6a1e130>
B:<myclass:0x6a1e130>
C:<myclass:0x6a1e130>
All pointing to the same block memory address
-----------------------------------cut cake Split line--------------------------------------------------------
However, this person (http://eschatologist.net/blog/?p=178) feels the tedious, so gives the following realization:
Java code
- @interface Somemanager:nsobject
- + (ID) Sharedmanager;
- @end
- /* Non-thread safe implementation */
- @implementation Somemanager
- + (ID) Sharedmanager {
- static id Sharedmanager = NIL;
- if (Sharedmanager = = nil) {
- Sharedmanager = [[Self alloc] init];
- }
- return Sharedmanager;
- }
- @end
- /* Thread-safe Implementation */
- @implementation Somemanager
- Static id Sharedmanager = NIL;
- + (void) Initialize {
- if (self = = [Somemanager class]) {
- Sharedmanager = [[Self alloc] init];
- }
- }
- + (ID) Sharedmanager {
- return Sharedmanager;
- }
- @end
-----------------------------------cut cake split line--------------------------------------------------------
since Apple introduced Grand Central Dispatch (GCD) (Mac OS 10.6 and iOS4.0), the use of GCD (Grand Central Dispatch) and arc (Automatic Reference counting) to implement a single case.
Java code
- + (schoolmanager *) sharedinstance
- {
- __strong static schoolmanager *sharedmanager;
-
- STATIC&NBSP;DISPATCH_ONCE_T&NBSP;ONCETOKEN;&NBSP;&NBSP;
- Dispatch_once (&oncetoken, ^{
- sharedmanager = [[schoolmanager alloc] init];
- });
-
- RETURN&NBSP;SHAREDMANAGER;&NBSP;&NBSP;
- }
function void Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block), where the first parameter predicate, This parameter is a predicate that checks whether the block of code represented by the second argument is called, and the second parameter is a block of code that will only be called once throughout the application. The code blocks in the Dispach_once function are only executed once and are thread-safe.
See the following article, with a macro implementation (HTTPS://GIST.GITHUB.COM/LUKEREDPATH/1057420):
Exampleclass.m
@implementation mysharedthing + (ID) sharedinstance{define_shared_instance_using_block (^{return [[Self alloc] init];}); @end
GCDSingleton.h
#define Define_shared_instance_using_block (BLOCK) \static dispatch_once_t pred = 0; \__strong static id _sharedobject = NIL; \dispatch_once (&pred, ^{\_sharedobject = block (); \}); \return _sharedobject; \
iOS single-instance mode detailed