The Singleton mode (Tool class) and Objective-C tool class in objective-c

Source: Internet
Author: User

The Singleton mode (Tool class) and Objective-C tool class in objective-c

Singleton is a design pattern that is often used in iOS development. As its name suggests, it creates a class which has only one instance object throughout the life cycle of the program, alloc init, copy, and other methods, or how many objects are created, only one space is opened in the memory from beginning to end until the program ends and is released by the system.

If six objects are created in different ways, but by printing their memory addresses, we can find that they share the same memory space.

 

Because it is often used in normal development, I define the method for creating a singleton as a macro and encapsulate it into a tool class, providing a class method to quickly create a singleton object; in addition, the tool class Singleton includes the creation method in MRC mode, ensuring that this tool class can still be used to quickly create a singleton object in MRC mode; this tool class is very convenient to use. You only need to import the header file in the class you need. The following is the implementation code:

1 // 2 // YYSharedModelTool. h 3 // SharedModel 4 // 5 // Created by Arvin on 15/12/21. 6 // Copyright©2015 Arvin. all rights reserved. 7 // 8 9 # ifndef YYSharedModelTool_h10 # define YYSharedModelTool_h11 12 //. h file 13 // ###: indicates the string 14 before and after splicing in the macro # define YYSharedModelTool_H (className) + (instancetype) shared ## className; 15 16 # if _ has_feature (objc_arc) // ARC environment 17 18 //. m file 19 # define YYSharedModelTool_M (className) \ 20/***** method for implementing Singleton in the ARC environment *****/\ 21 + (instancetype) shared # className {\ 22 return [[self alloc] init]; \ 23} \ 24 \ 25-(id) copyWithZone :( nullable NSZone *) zone {\ 26 return self; \ 27} \ 28 \ 29 + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ 30 static id instance; \ 31 static dispatch_once_t onceToken; \ 32 dispatch_once (& onceToken, ^ {\ 33 instance = [super allocWithZone: zone]; \ 34}); \ 35 return instance; \ 36} 37 38 # else // MRC environment 39 40 //. m file 41 # define YYSharedModelTool_M (className) \ 42 \ 43 + (instancetype) shared # className {\ 44 return [[self alloc] init]; \ 45} \ 46 \ 47-(id) copyWithZone :( nullable NSZone *) zone {\ 48 return self; \ 49} \ 50 \ 51 + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ 52 static id instance; \ 53 static dispatch_once_t onceToken; \ 54 dispatch_once (& onceToken, ^ {\ 55 instance = [super allocWithZone: zone]; \ 56}); \ 57 return instance; \ 58} \ 59/***** the following three methods must be rewritten in the MRC environment: *****/\ 60-(oneway void) release {\ 61 \ 62} \ 63-(instancetype) retain {\ 64 return self; \ 65} \ 66-(instancetype) autorelease {\ 67 return self; \ 68} 69 70 # endif71 72 # endif/* YYSharedModelTool_h */

END! You are welcome to leave a message and learn together...

 

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.