1. Single case design mode (Singleton)
* Ensure that the object created by a class will always have only one
2. Role
* Save memory overhead.
* If some of the data is used throughout the program, only the same resources (to ensure that the data you access are the same)
* Generally, tool design is suitable for single case mode
3. Implement
* MRC
* ARC
SoundTool.h
1 #import <Foundation/Foundation.h>23@interface Soundtool:nsobject < Nscopying>45 + (instancetype) Sharesoundtool; 6 7 @end
View Code
soundtool.m
#import "SoundTool.h"@implementationSoundtoolStatic ID_instance =Nil;+ (Instancetype) Allocwithzone: (struct_nszone *) zone{if(_instance = =Nil) { Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[Super Allocwithzone:zone]; }); } return_instance;}+(instancetype) sharesoundtool{return[[Self alloc] init];}-(instancetype) init{Staticdispatch_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 for non-arc use-(OneWayvoid) release{}-(instancetype) retain{return_instance;}-(Nsuinteger) retaincount{return 1;}View Code
4. Recommended packaging for use as a macro
Relive-Singleton mode