IOS Singleton mode

Source: Internet
Author: User
To save system resources, we sometimes need to ensure that only one instance is unique for a class in the system. after this unique instance is created successfully, we cannot create another object of the same type, all operations can only be performed based on this unique instance.

Singleton mode:

Why is Singleton and Singleton modes used? The following is an example.

For example, Windows Task Manager, we can make such an attempt, in Windows, right-click the "task bar" and choose "start Task Manager" multiple times in the pop-up menu. can you open multiple task manager Windows? Generally, no matter how many times we start task management, only one task manager window is displayed in Windows. that is to say, in a Windows system, the Task Manager is unique. Why is it designed like this? We can analyze it from the following two aspects: first, if multiple windows are displayed and the content of these windows is completely consistent, all of them are repeated objects, this will inevitably waste system resources, the task manager needs to obtain a lot of information during system operation, which consumes a certain amount of system resources, including CPU resources and memory resources. it is shameful to waste resources, in addition, there is no need to display multiple windows with the same content. Second, if the content of the pop-up windows is inconsistent, the problem becomes more serious, this means that the system resource usage and process, service, and other information are in multiple states at A certain moment. for example, in the job manager window A, the "CPU usage" is 10%, window B shows that "CPU usage" is 15%. which one is true? This is purely a "teasing" user, which brings misunderstandings to users and is not desirable. It can be seen that the Windows Task Manager is only important in the system.


Back to actual development, we often encounter similar situations. to save system resources, we sometimes need to ensure that a class in the system has only one unique instance. after this unique instance is created successfully, we cannot create another object of the same type. all operations can only be performed based on this unique instance. To ensure the uniqueness of objects, we can implement it through the Singleton mode, which is the motivation of the Singleton mode.


Below are several examples:

Here, a Person class inheritance and NSObject are created

. H file

// Person. h

// Singleton

// Copyright (c) 2014 summer2014mht@sina.com. All rights reserved.


# Import

@ Interface Person: NSObject

// Singleton: A class has only one instance. When an object is created externally, alloc cannot be used. (as long as alloc. 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, and it is a + number method.

// Default, standard, main, and shared start with Singleton

+ (Person *) sharedPerson;

@ End


. M file

// Person. m

// Singleton

// Copyright (c) 2014 summer2014mht@sina.com. All rights reserved.

# Import "Person. h"


@ Implementation Person

/*

// Write method 1

+ (Person *) sharedPerson

{

// Declare it as static to ensure that the space of the variable is not recycled during the running of the program, and that the variable is initialized only once.

// The space of a singleton is not recycled during the running of the program. Therefore, use the singleton with caution. otherwise, memory accumulation may occur.

Static Person * person = nil;

@ Synchronized (self ){

If (person = nil ){

Person = [[Person alloc] init];

}

}

Return person;

}

*/

// Write method 2 (recommended)

+ (Person *) sharedPerson

{

Static Person * person = nil;

Static dispatch_once_t onceToken;

Dispatch_once (& onceToken, ^ {

Person = [[Personalloc] init];

});

Return person;

}

@ End


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.