Single-Instance class

Source: Internet
Author: User

The singleton pattern means that there is only one instance. The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. This class is called a singleton class.

1. Key points of the singleton mode:

  It is obvious that there are three main points of the singleton pattern; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.

2. Advantages of Singleton mode:

1. Instance control: Singleton prevents other objects from instantiating copies of their own Singleton objects, ensuring that all objects have access to unique instances. 2. Flexibility: Because classes control the instantiation process, classes can be more flexible in modifying the instantiation process Singleton mode in iOSTo implement a singleton class in objective-c, you need to do at least the following four steps:
1, for a singleton object to implement a static instance, and initialize, and then set to nil,
2. Implement an instance construction method to check if the static instance declared above is nil, and if it is new and returns an instance of this class,
3, rewrite the Allocwithzone method, to ensure that other people directly use Alloc and Init to try to gain a new strength when the time does not produce a new instance,
4, the appropriate implementation of Allocwithezone,copywithzone,release and Autorelease. The following is an example of surveyruntimedata: static surveyruntimedata *sharedobj = nil; First step: static instance, and initialize.
@implementation Surveyruntimedata
+ (surveyruntimedata*) sharedinstance//Second step: instance construction checks if the static instance is nil
{
@synchronized (self)
{
if (sharedobj = = nil)
{
[[Self alloc] init];
}
}
return sharedobj;
}

+ (ID) Allocwithzone: (Nszone *) zone//step three: Overriding the Allocwithzone method
{
@synchronized (self) {
if (sharedobj = = nil) {
Sharedobj = [Super Allocwithzone:zone];
return sharedobj;
}
}
return nil;
}

-(ID) Copywithzone: (Nszone *) zone//Fourth step
{
return self;
}

-(ID) retain
{
return self;
}

-(unsigned) retaincount
{
return Uint_max;
}

-(OneWay void) release
{

}

-(ID) autorelease
{
return self;
}
-(ID) init
{
@synchronized (self) {
[Super init];//tend to put some variables to initialize.
return self;
}
}

@end

Single-Instance class

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.