IOS Single case
1. What is a single case?
is a class that has only one instance object in the system. This instance object is accessed through one of the global portals.
2. Application Scenarios?
Used to expect only one instance of a class, without running a class with more than two instances.
The A:ios SDK is applied to the Singleton mode class:
Shareapplication Unified Management of UIApplication
Nsuserdefaults's Standarduserdefaults Unified management user profile,
Nsfilemanager's Defaultmanager is responsible for the management of physical documents,
Defaultcenter is responsible for global message distribution in Nsnoficationcenter.
B: Custom Scene: Theme management, download management, transfer data;
3. How to implement and use?
ARC + GCD Implementation
Xx.h
1 + (Arcsingleton *) sharedinstance;
Xx.m
1+ (Arcsingleton *) Sharedinstance2 {3 //14 StaticArcsingleton *sharedinstance =Nil;5 //26 Staticdispatch_once_t Oncetoken;//Lock7 //38Dispatch_once (&oncetoken, ^ {//call at most once9Sharedinstance =[[Self alloc] init];Ten }); One returnsharedinstance; A } - - //This init method is called when the Singleton is used for the first time. the- (ID) Init - { -Self =[Super init]; - + if(self) { - //usually do some related initialization tasks here + } A at returnSelf ; -}
1. Declaring a static variable to save an instance of the class, ensuring its global availability in the class
·2. Declares a static variable dispatch_once_t, which ensures that the initializer code executes only once
·3. Use the Grand Central Dispatch (GCD) to perform the initialization of the block. This is the key to the singleton pattern: Once the class has been initialized, the initializer will never be called again.
4. Single-Instance Factory
Managing a large number of individual cases in a project
The essence of the factory method pattern is "to define an interface to create an object, but let the class implementing the interface decide which class to instantiate." The factory method defers instantiation of a class to a subclass. ”
5. Module Management System
Unified Management
---------------------------------------------
Reference
The evolutionary http://blog.jobbole.com/56439/of the single case model of 1.iOS design pattern reflection
2.iOS design Mode (02): Single Case mode http://beyondvincent.com/blog/2013/05/09/20/
A single example of iOS design patterns