Three examples and three examples are used.
Singleton: A class has only one instance. When creating objects externally, alloc cannot be used. (As long as alloc is used, it will open up space in the heap area, which means there are multiple objects.) Therefore, we need to provide a method to create an object:
1. plus sign Method
2. the return value type is the current class.
3. The method name starts with default, standared, main, and shared + the current class name
The following uses the Person class as an example.
Declare in. h file
+ (Person *) sharePerson;
Implementation in the. m file
The first mode (this mode is used by a single thread)
+ (Person *) sharePerson {
Declared as static, ensure that the variable will not be recycled during the program running, and only initialize once
The space of a single instance is not recycled during the running of the program. Exercise caution when using the instance. Otherwise, memory accumulation may occur.
Static Person * person = nil;
If (! Person ){
Person = [[Person alloc] init];
}
Return person;
}
Second mode (multithreading uses this mode)
+ (Person *) sharePerson {
Multithreading
StaticPerson * person = nil;
@ Synchronized (self ){
If (person = nil ){
Person = [[Personalloc] init];
}
}
Return person;
}
Third mode (single thread and multi-thread can be used)
+ (Person *) sharePerson {
StaticPerson * person = nil;
Staticdispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
Ensure that the task is executed only once, whether multithreading or single thread
Person = [[Personalloc] init];
});
Return person;
}
How to write the code in java Singleton mode?
I have pasted my article from my blog. I should have a clear explanation of the singleton mode:
The Singleton mode is very common in our daily projects. When we need such an object in the project, this object can only have one instance in the memory, in this case, we need to use a singleton.
Generally, the singleton mode includes the following:
1. Hunger-type Singleton
Public class Singleton {
Private Singleton (){};
Private static Singleton instance = new Singleton ();
Public static Singleton getInstance (){
Return instance;
}
}
This is the simplest Singleton, which is the most common and reliable! Its only drawback is that it cannot complete delayed loading-that is, when the system has not used this Singleton, the singleton will be loaded into the memory.
Here we can perform a test like this:
Modify the above Code:
Public class Singleton {
Private Singleton (){
System. out. println ("createSingleton ");
};
Private static Singleton instance = new Singleton ();
Public static Singleton getInstance (){
Return instance;
}
Public static void testSingleton (){
System. out. println ("CreateString ");
}
}
We test it in another test class (in this example, all tests passed Junit)
Public class TestSingleton {
@ Test
Public void test (){
Singleton. testSingleton ();
}
}
Output result:
CreateSingleton
CreateString
We can note that in this Singleton, even if we do not use the singleton class, it is still created. This is of course what we do not want to see, so we have the following Singleton.
2. Lazy Singleton
Public class Singleton1 {
Private Singleton1 (){
System. out. println ("createSingleton ");
}
Private static Singleton1 instance = null;
Public static synchronized Singleton1 getInstance (){
Return instance = null? New Singleton1 (): instance;
}
Public static void testSingleton (){
System. out. println ("CreateString ");
}
}
Synchronization is required when the preceding Singleton obtains an instance. If synchronization is not added, when thread 1 completes the create Singleton operation in a multi-threaded environment, before assigning values, thread 2 may judge
The disconnected instance is empty. At this time, thread 2 also exists.
Java Singleton mode? How to Write
Class AAA {
Private static AAA a = null;
Private AAA (){
}
Public static AAA getaskact (){
If (= null ){
A = new AAA ();
}
Return;
]
}
The key is the private constructor, and then an entry that can get the object.