Singleton, Singleton Mode

Source: Internet
Author: User

Singleton, Singleton Mode

 

# Import <Foundation/Foundation. h>/*** Singleton is a common design pattern in iOS, that is, this class only has one instance. use Cases: You can upload data from front to back or from back to front. * // *** use main, default. standard, shared */@ interface Singleton: NSObject + (Singleton *) sharedSingleton; @ property (nonatomic, retain) NSString * textFiledText; // Save the input text in the input box of the first interface @ property (nonatomic, retain) NSString * secondTextFiledText; // Save the text entered in the second interface @ end @ implementation Singleton // declare it as a static variable, only initialize once, and the space will not be released during the running of the program. // For a Singleton, make sure that the space of the object is not recycled during the running of the program. space cannot be recovered in time. static Singleton * singleton = nil; + (Singleton *) sharedSingleton {// you only need to create an object once if (singleton = nil) {singleton = [[Singleton alloc] init];} return singleton;} @ end




What is the JAVA Singleton mode?

Wait for two Singleton modes:
Hungry Chinese Style
Class Singleton {
Private static Singleton instance = new Singleton ();
Private Singleton (){}
Static Singleton getInstance (){
Return instance;
}
}
Lazy
Class Singleton {
Private static Singleton instance = null;
Private Singleton (){}
Static Singleton getInstance (){
If (instance = null)
Instance = new Singleton ();
Return instance;
}
}

C ++ Singleton Mode

1. Private Constructor

This means that the Singleton constructor can be called to create instances only in Singleton's member functions. In addition to Singleton, instances of Singleton objects cannot be created.

2. The GetInstance method is defined in the Code. The GetInstance method can only be used to obtain instances of the Singleton object. The Singleton method controls the Singleton object.

First, Singleton has
Static Singleton * instance; // unique instance

Singleton * Singleton: instance = NULL;
The initialization here is NULL.

Singleton * Singleton: GetInstance ()
{
If (instance = NULL)
{
Instance = new Singleton ();
}
Return instance;
}

The above function implements Singleton through instance.

When GetInstance is called for the first time and the instance is NULL
Instance = new Singleton ();
Save the newly created instance to the static member instance and return this pointer.

When GetInstance is called for the second to the nth time, the instance is directly returned because the instance is not empty. That is, the instance created by calling GetInstance for the first time.

So we can achieve this by using a single instance.

That is to say, the Singleton object instance will only be created once, that is, there is only one Singleton instance in the memory, that is, a single instance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.