The Singleton design mode and Singleton macro extraction in OC, And the OC design mode macro Extraction

Source: Internet
Author: User

The Singleton design mode and Singleton macro extraction in OC, And the OC design mode macro Extraction

 

 

 

1 // when an object needs to be reused and frequently used, you can use the singleton design mode for the object. 2 // The Singleton design is actually the allocWithZone inside multiple alloc, override this method 3 4 # pragma Person. h File 5 6 # import <Foundation/Foundation. h> 7 @ interface Person: NSObject <NSCopying, NSMutableCopying> 8 + (instancetype) sharePerson; // provide a class factory method for creating a singleton object for the Class 9 @ end 10 11 # pragma Person. m file 12 13 // 14 // Person. m 15 // Singleton Design 16 // 17 // Created by KANG Sheng Qiu on 15/11/26. 18 // Copyright (c) 2015 Kang shengqiu. all rights reserved. 19 // 20 21 # import "Person. h "22 23 24 25 @ implementation Person 26 27 + (instancetype) sharePerson 28 {29 Person * instance = [[self alloc] init]; // The Singleton design can start with 30 return instance by using the allocWithZone method in alloc; 31} 32 static Person * _ instance = nil; 33 // This method determines whether the created bucket address is 34 + (instancetype) allocWithZone (struct _ NSZone *) zone 35 {36 // if _ instance is equal to nil, call alocWithZone of the parent class to create an object and assign it a value of 37 if (_ instance = nil) after initialization) {38 _ instance = [[super allocWithZone: zone] init]; 39} 40 41 // If _ instance is not equal to nil, that is, it has already pointed to an object, you can directly return the address, in this way, the newly created object also points to the object 42 return _ instance of the same address; 43} 44 45-(id) copyWithZone :( NSZone *) zone 46 {47 // copy is called by an object. In a single instance, an object already exists and you do not need to create an object. directly return the object pointer to 48 return _ instance; 49} 50 51-(id) mutableCopyWithZone :( NSZone *) zone 52 {53 // The same as mutableCopy, you can directly return the Object Pointer 54 return _ instance; 55} 56 57 58 59 // in MRC, you also need to override release, retain, retainCount 60-(oneway void) release 61 {62 // to ensure that the singleton object is not released before the end of the program, \ 63 nothing is written here 64} 65-(instancetype) retain 66 {67 // directly return the singleton object address 68 return _ instance; 69} 70-(NSUInteger) retainCount 71 {72 // a special value can be returned, to remind other programmers of 73 // return MAXFLOAT; 74 return UINT64_MAX; 75} 76 77 @ end 78 79 80 # pragma-macro extraction for Singleton mode 81 // create a header file (Singleton. h) 82 83 // you can use interfaceSingleton to replace the Person class Singleton class factory method statement 84 # define interfaceSingleton (name) + (instancetype) share # name; 85 86 // if the current project is under ARC 87 # if _ has_feature (objc_arc) 88 // ARC 89 # define implementationSingleton (name) \ 90 + (instancetype) share # name \ 91 {\ 92 name * instance = [[self alloc] init]; \ 93 return instance; \ 94} \ 95 static name * _ instance = nil; \ 96 + (instancetype) allocWithZone :( struct _ NSZone *) zone \ 97 {\ 98 if (_ instance = nil) {\ 99 _ instance = [[super allocWithZone: zone] init]; \ 100} \ 101 return _ instance; \ 102} \ 103-(id) copyWithZone :( NSZone *) zone \ 104 {\ 105 return _ instance; \ 106} \ 107-(id) mutableCopyWithZone :( NSZone *) zone \ 108 {\ 109 return _ instance; \ 110} 111 # else112 // MRC113 # define implementationSingleton (name) \ 114 + (instancetype) share # name \ 115 {\ 116 name * instance = [[self alloc] init]; \ 117 return instance; \ 118} \ 119 static name * _ instance = nil; \ 120 + (instancetype) allocWithZone :( struct _ NSZone *) zone \ 121 {\ 122 if (_ instance = nil) {\ 123 _ instance = [[super allocWithZone: zone] init]; \ 124} \ 125 return _ instance; \ 126} \ 127-(id) copyWithZone :( NSZone *) zone \ 128 {\ 129 return _ instance; \ 130} \ 131-(id) mutableCopyWithZone :( NSZone *) zone \ 132 {\ 133 return _ instance; \ 134} \ 135-(oneway void) release \ 136 {\ 137} \ 138-(instancetype) retain \ 139 {\ 140 return _ instance; \ 141} \ 142-(NSUInteger) retainCount \ 143 {\ 144 return MAXFLOAT; \ 145} 146 // compile the final condition 147 # endif148 149 by extracting the Code related to the singleton mode into a header file. In the future, you only need to import the code to the header file in the class, and pass in the class name 151 152 # pragma-the Person after the singleton macro extraction. h文 153 154 # import <Foundation/Foundation. h> 155 # import "Singleton. h "156 @ interface Person: NSObject <NSCopying, NSMutableCopying> 157 158 interfaceSingleton (Person) // macro definition. during compilation, the macro name is replaced with + (instancetype) sharePerson; 159 // The parameter is passed to tell the macro to replace the time with the method declaration. What is the name after share 160 161 @ end162 163 164 # pragma-the Person after the singleton macro extraction. m file 165 166 # import "Person. h "167 168 @ implementation Person169 170 // use a macro definition to get an alias for the code in the implementation file. In the implementation, write the macro name + the current class name, and set the 171 implementationSingleton (Person) 172 @ endView Code

 

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.