Review-singleton mode, review-Mode

Source: Internet
Author: User

Review-singleton mode, review-Mode

1. Singleton)

* Ensure that only one object is created for a class.

2. Role

* Saves memory overhead.

* If some data is used in the entire program, you only need to use the same resource (ensure that the data accessed by everyone is the same)

* Generally, the tool class is designed as a singleton mode.

3. Implementation

* MRC

* ARC

SoundTool. h

1 # import <Foundation/Foundation. h> 2 3 @ interface SoundTool: NSObject <NSCopying> 4 5 + (instancetype) Explain SoundTool; 6 7 @ endView Code

SoundTool. m

# Import "SoundTool. h "@ implementation SoundToolstatic id _ instance = nil; + (instancetype) allocWithZone :( struct _ NSZone *) zone {if (_ instance = nil) {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [super allocWithZone: zone] ;});} return _ instance ;}+ (instancetype) export soundtool {return [[self alloc] init];}-(instancetype) init {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [super init];}); return _ instance;} + (instancetype) copyWithZone :( struct _ NSZone *) zone {return _ instance;} + (instancetype) mutableCopyWithZone :( struct _ NSZone *) zone {return _ instance;} // The following three are not used by ARC-(oneway void) release {}-(instancetype) retain {return _ instance;}-(NSUInteger) retainCount {return 1 ;}View Code

4. It is recommended to package it into a macro.

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.