iOS Development-Singleton mode

Source: Internet
Author: User

iOS Development-single case mode (ARC)

First, simple description:Design Patterns: Years of software development, summed up a set of experience, methods and toolssecond, the single case mode description(1) The role of the singleton mode: can guarantee in the program running process, a class has only one instance, and the instance is easy for outside access, so as to conveniently control the number of instances, and save system resources. (2) The use of Singleton mode: in the entire application, share a resource (this resource only need to create initialization 1 times), should let this class created by the object is always only one.three. Design Ideas (1) Always allocate only one piece of memory to create objects (2) Provide a class method that returns a unique internal variable (3) It is best to ensure that the Init method is initialized only onceiv. Code Examples
1 #import <Foundation/Foundation.h>23@interface  yxaudiotool:nsobject  45 +(instancetype) Shareaudiotool; 6 7 @end

1 #import "YXAudioTool.h"2 3 @interfaceYxaudiotool ()4 5 //@property (nonatomic,strong) nsmutabledictionary * MUSCIS;6 7 @end8 9 @implementationYxaudiotoolTen  One Static ID_instance; A  --(instancetype) init - { the     Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ -         if(Self = =[Super Init]) { -             //load the required music resources +         } -     }); +     returnSelf ; A } at  -+ (Instancetype) Allocwithzone: (struct_nszone *) zone{ -     Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ -_instance =[Super Allocwithzone:zone]; -     }); in     return_instance; - } to  ++(instancetype) shareaudiotool{ -     Staticdispatch_once_t Oncetoken; theDispatch_once (&oncetoken, ^{ *_instance =[[Self alloc]init]; $     });Panax Notoginseng     return_instance; - } the  ++ (Instancetype) Copywithzone: (struct_nszone *) zone{ A     return_instance; the } +  -+ (Instancetype) Mutablecopywithzone: (struct_nszone *) zone{ $     return_instance; $ } -  --(Instancetype) Copywithzone: (struct_nszone *) zone{ the     return_instance; - }Wuyi  the-(Instancetype) Mutablecopywithzone: (struct_nszone *) zone{ -     return_instance; Wu } -  About @end

v. DescriptionOverride Allocwithzone: Method to control memory allocation. Because the method is called internally by Alloc, each time the Allocwithzone: method is called, the system creates a new memory space. Alloc method: Always allocate only once in the memory Init method: Ensure that all MP3 data is loaded only once.using the Dispatch_once one- time Code, the entire program runs and executes only once. The default is thread-safe 6. ExpansionThe singleton pattern can be divided into the lazy model and the vicious Han mode. Another pattern of our most common lazy patterns: the bad-han mode when a class is loaded into the OC Runtime Environment (memory), it calls the + (void) load once (a class will only be loaded once) the bad Han pattern is thread safe, Because the virtual machine is guaranteed to be loaded only once, there is no concurrency when the class is loaded.
1 #import "YXAudioTool.h"2 3 @interfaceYxaudiotool ()4 5 //@property (nonatomic,strong) nsmutabledictionary * MUSCIS;6 7 @end8 9 @implementationYxaudiotoolTen  One Static ID_instance; A  --(instancetype) init - { theSelf =[Super init]; -     if(self) { -          //load the required music resources -     } +     returnSelf ; - } +  A+ (Instancetype) Allocwithzone: (struct_nszone *) zone{ at_instance =[Super Allocwithzone:zone]; -     return_instance; - } -  -+(void) load{ -_instance =[[Self alloc]init]; in } -  to+(instancetype) shareaudiotool{ +     return_instance; - } the  *+ (Instancetype) Copywithzone: (struct_nszone *) zone{ $     return_instance;Panax Notoginseng } -  the+ (Instancetype) Mutablecopywithzone: (struct_nszone *) zone{ +     return_instance; A } the  +-(Instancetype) Copywithzone: (struct_nszone *) zone{ -     return_instance; $ } $  --(Instancetype) Mutablecopywithzone: (struct_nszone *) zone{ -     return_instance; the}

iOS Development-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.