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
Initialization is very simple here is not an example, then the implementation of the singleton mode in the Initialize is how to complete it?
Static Userhelper * helper = nil;
+ (void) Initialize {
if ([self class] = = [Userhelper class]) {
Helper = [[Userhelper alloc] init];
}
}
Just imagine, this is not very simple to implement a singleton mode AH
So let's just say, by the way, the singleton pattern, I understand that there is only one instance object in a class, look at the more official 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.
Steps to create a single case
(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
Several implementation methods of single case
1> non-thread-safe notation
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
If (helper = = nil) {//Determine if the object exists, and if it does not exist, create the object
Helper = [[Userhelper alloc] init];
}
return helper;
}
2> thread-safe notation 1
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
@synchronized (self) {//plus a mutex so that no longer accesses the method at the same time, create two instances, resulting in a singleton error, the improvement of the previous method
if (helper = = nil) {
Helper = [[Userhelper alloc] init];
}
}
return helper;
}
3> Thread-safe notation (Apple recommends, mainly with this, we also use this for the longest)
Static Userhelper * helper = nil;
+ (Userhelper *) Shareduserhelper {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Helper = [[Userhelper alloc] init];
});
return helper;
}
There is a kind of the above introduction, the way to create a single case many kinds of OH ~ ~ You remember a few?
load&initialize& Single-Case mode