Swift singleton and Swift
Swift Singleton Statement by Wu xueying
override func viewDidLoad() { super.viewDidLoad() let instance = SingletonClass.shared } class SingletonClass { class var shared: SingletonClass { struct Static { static let instance: SingletonClass = SingletonClass() } return Static.instance }}
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.
What is the difference between the singleton method and the general method?
I feel that writing this will prevent concurrent operations from causing multiple instantiation.
For example:
Public class Singleton {
Private Singleton (){}
Private static Singleton instance = null;
Public static Singleton getInstance (){
If (instance = null ){
Instance = new Singleton ();
}
Return instance;
}
}
When Singleton. getInstance () is concurrently called, new Singleton () may be executed multiple times. This problem can also be avoided if it is changed to the following format:
Public class Singleton {
Private Singleton (){}
Private static Singleton instance = new Singleton ();
Public static Singleton getInstance (){
Return instance;
}
}