IOS Singleton mode

Source: Internet
Author: User

Single-case mode :

Why use Singleton , What is the purpose of the singleton mode ? Let's give an example to explain

To give an example that everyone knows--WindowsTask Manager, and we can do one such try, inWindowsOf"Task bar"Multiple clicks on the right-click popup menu"Start Task Manager", see if you can open multiple Task Manager Windows? Typically, no matter how many times we start the task management,WindowsThe system can always eject only one Task Manager window, which meansWindowsIn the system, Task Manager has uniqueness. Why design it like this? We can analyze from the following two aspects: first, if you can pop up multiple windows, and the contents of these windows are identical, all are duplicate objects, which will waste the system resources, the task Manager needs to obtain the system runtime of the many information, the acquisition of this information needs to consume some system resources, includingCPUResources and memory resources, and so on, waste is shameful, and there is no need to display multiple content exactly the same window; second, if the pop-up of multiple window content is inconsistent, the problem is more serious, which means that in a moment system resource usage and processes, services and other information there are multiple states, such as the Task Manager windowAShow"CPUUsage rate"For10%WindowBShow"CPUUsage rate"For15%, in the end, which is true? This is purely"Molested"Users, to bring misunderstanding, but not to take. Thus, ensure thatWindowsIt is important that task Manager has and only one in the system.


Back to the actual development, we often encounter similar situation, in order to save system resources, sometimes need to ensure that a class in the system only one instance, when the unique instance is created successfully, we can no longer create another object of the same type, all operations can only be based on this unique instance. In order to ensure the uniqueness of the object, we can use the singleton mode, which is the motive of the singleton mode.


Here are a few ways to style the following singleton:

Here you create a person class inheritance with NSObject

. h file

Person.h

Single case mode Singleton

Copyright (c) year [email protected] All rights reserved.


#import<Foundation/Foundation.h>

@interface Person:NSObject

// singleton : There is only one instance of a class , and you cannot use alloc when creating an object externally . Just Alloc. will open up space in the heap area , which means there are multiple objects ).

// so we're going to provide a way to create an object . And it's a + number method.

Default, Standard, Main, GKFX all start with a singleton

+ ( person *) Sharedperson;

@end


. m file

Person.m

Single case mode Singleton

Copyright (c) year [email protected] All rights reserved.

#import"Person.h"


@implementation Person

/*

// writing a

+ (person *) Sharedperson

{

// declared as static to ensure that the variable is not reclaimed during program run time . and guaranteed to initialize only once.

// Singleton space is not recycled during program run , so use a single case sparingly , otherwise it will cause memory to accumulate

static person *person = nil;

@synchronized (self) {

if (person = = nil) {

person = [[Person alloc] init];

}

}

return person;

}

*/

// two (recommended)

+ ( person *) Sharedperson

{

Static Person *person = nil;

Static dispatch_once_t Oncetoken;

dispatch_once (&oncetoken, ^{

person = [[personalloc] init];

});

return person;

}

@end


IOS Singleton mode

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.