A single example of IOS design patterns

Source: Internet
Author: User

The singleton is nothing more than just one instance, which ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, it is possible to pass data between different windows.

The singleton pattern is the simplest design pattern in all patterns, which is intended to make an object a unique instance of the system.

When it comes to the single-case model, let's say his pros and cons: The advantage is that you can let the program other classes to use it, simple, you can implement the data transfer between the Ono window, of course, there are advantages, there must be shortcomings, the disadvantage is that in a single case does not release memory, so generally a program can only have 4 single Will cause the system to collapse.

To implement a singleton class in one OC, you must implement 4 steps

(1): For a singleton object, the implementation of a static instance, and initialization, set to nil, must be a plus, class method;

(2): Implement an instance construction method to check if the static instance declared above is nil, and if yes, new and return an instance of this class

(3): Rewrite the Initwithzone method to ensure that others directly use alloc and Init to try to obtain a new instance without creating a new instance,

(4): proper implementation of Allocwithezone,copywithzone,release and Autorelease

Example: Create a singleton function for Rootviewcontroller:

static Rootviewcontroller *sharerootviewcontroller = nil;
+ (Rootviewcontroller *) sharedcontroller{
       @synchronized (self) {
               if (Sharerootviewcontroller = = nil) {
                       Sharerootviewcontroller = [[[Self alloc] init] autorelease];
                }
        }
       return sharerootviewcontroller;
}

+ (ID) allocwithzone:(Nszone *) zone{
       @synchronized (self) {
               if (Sharerootviewcontroller = = nil) {
                  Sharerootviewcontroller = [Super Allocwithzone:zone];
                   returnSharerootviewcontroller;
                }
            }
       return nil;
}

If the singleton object is actually accessed by multiple threads, then it is necessary to ensure that his thread is secure.

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.