Looking back at the load and initialize methods, there are a few things to note.
Load method and Initialize method analysis:
1.Load Method :
1> executes this function when the class is referenced into the program
2> the Load method of a class does not specify [super Load], the parent class receives the call, and precedes the child class.
The load of the 3> category will also receive calls, but in order after the main class's load call.
2,Initialize method :
The natural invocation of 1> initialize is the first time that the current class is actively used
2> and load are different, even if the subclass does not implement the Initialize method, the implementation of the parent class is inherited to call it over. Note that before this, the method of the parent class has been executed once, and no super call is required.
3, load and initialize have a lot of common characteristics, the following simple list:
1> the system will call at most once without considering the developer's active use
2> if both the parent class and the child class are called, the call of the parent class must precede the child class
3> to create the right environment for the application to run ahead of time
4> do not rely heavily on these two methods when used, unless it is really necessary to
Due to these characteristics of initialize, the application is slightly more extensive than load. Can be used to do some initialization work, or a single example mode of implementation
4. Single case mode
1> concept
Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.
2> Writing steps
(1) Create a class method that returns an object instance. Start with shared default current.
(2) Create a global variable to hold a reference to the object
(3) Determine if the object exists, if it does not exist, create the object
3> non-thread-safe notation
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
if (helper = = nil) {
Helper = [[Userhelper alloc] init];
}
return helper;
}
4> thread-safe notation 1
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
@synchronized (self) {
if (helper = = nil) {
Helper = [[Userhelper alloc] init];
}
}
return helper;
}
5> thread-Safe notation 2
+ (void) Initialize {
if ([self class] = = [Userhelper class]) {
Helper = [[Userhelper alloc] init];
}
}
6, Thread safety notation 3 (Apple recommended, mainly use this)
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Helper = [[Userhelper alloc] init];
});
return helper;
}
7, MRC full implementation of a single example (understanding)--here first do not introduce.
iOS Development-oc-Singleton mode