01-11 Single-Case mode

Source: Internet
Author: User

  1. + (dlpontlerexple*) shareindence

    {

    static dlpontlerexple *dil = nil;

    if (dil==nil) {

    Dil = [[[[dlpontlerexple class]alloc]init];

    }

    return Dil;

    }

  2. + (dlpontlerexple *) shareindence;

  3. UnderstandSingleton (single-state) modeThe singleton design pattern ensures that only one instance of a given class exists, and that the instance has a globally unique access point. It usually uses lazy loading to create it the first time it is used. Note: This mode is used extensively by Apple. For example: [Nsuserdefaults standarduserdefaults], [uiapplication sharedapplication], [UIScreen mainscreen], [NSFileManager Defaultmanager], all of these methods return a singleton object. You'll probably wonder why you're so concerned. Is there more than one instance of a class?  After all, the code and the memory are cheap, right? In some cases, only one instance seems very reasonable. For example, you don't need to have multiple instances of logger unless you want to write multiple log files. Or a global configuration processing class: It is easy to implement thread-safe access to shared instances, such as a configuration file, with many classes modifying the file at the same time.How to use singleton modeFirst, take a look at the following diagram: The diagram above depicts a class with a single attribute (which is a singleton) and a sharedinstance,init of two methods. When the client sends the SHAREDINSTANCE message for the first time, the instance property has not been initialized, so you need to create a new instance and return its reference. The next time you call Sharedinstance, instance does not need any initialization to return immediately. This logic guarantee always has only one instance. You will then use this pattern to create a class that manages all the album data. You will notice that there is a group of APIs in the project where you can put all the classes that provide services to your app. In this group, create a new class with the Ios\cocoa touch\objective-c class template, name it Libraryapi, and set the parent class to NSObject. Open LibraryAPI.h and replace its contents with the following code
      1. @interfaceLibraryAPI: NSObject
      2. + (libraryapi*) sharedinstance;
      3. @end
    Now open LIBRARYAPI.M, and insert the following method after the @implementation line:
    1. + (libraryapi*) sharedinstance
    2. {
    3. //1
    4. static Libraryapi *_sharedinstance = nil;
    5. //2
    6. static dispatch_once_t oncepredicate;
    7. //3
    8. Dispatch_once (&oncepredicate, ^{
    9. _sharedinstance = [[Libraryapi alloc] init];
    10. });
    11. return _sharedinstance;
    12. }
    In this short approach, there are some points to note: 1. Declare a static variable to hold an instance of the class, ensuring its global availability in the class.  2. Declare a static variable dispatch_once_t, which ensures that the initializer code executes only once 3. Use Grand Central Dispatch (GCD) to perform a block that initializes the LIBRARYAPI variable. This It is the key to the singleton pattern: Once the class has been initialized, the initializer will never be called again.  The next time you call Sharedinstance, the code in the Dispatch_once block will not execute (because it has been executed once) and you will get an instance of the original already initialized. Note: In order to learn more about GCD and how it is used, please see the Site Guide multithreading and Grand Central Dispatch and how to use Blocks.

01-11 Single-Case 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.