1 #import "Singleton.h"
2
3
4 @implementation Singleton
5 static Singleton* instance;
6
7 - (id)init
8 {
9 self = [super init];
10 if (self) {
11 // Initialization code here.
12 }
13
14 return self;
15 }
16
17 + (id)alloc
18 {
19 @synchronized([Singleton class])
20 {
21 instance = [super alloc];
22 return instance;
23 }
24 return nil;
25 }
26
27 +(Singleton*)sharedInstance
28 {
29 @synchronized([Singleton class])
30 {
31 if(!instance)
32 {
33 [[self alloc] init];
34 }
35
36 return instance;
37 }
38 return nil;
39 }
40
41 - (void)sayHello
42 {
43 NSLog(@"Hello world!");
44 }
45
46 @end
2
3
4 @implementation Singleton
5 static Singleton* instance;
6
7 - (id)init
8 {
9 self = [super init];
10 if (self) {
11 // Initialization code here.
12 }
13
14 return self;
15 }
16
17 + (id)alloc
18 {
19 @synchronized([Singleton class])
20 {
21 instance = [super alloc];
22 return instance;
23 }
24 return nil;
25 }
26
27 +(Singleton*)sharedInstance
28 {
29 @synchronized([Singleton class])
30 {
31 if(!instance)
32 {
33 [[self alloc] init];
34 }
35
36 return instance;
37 }
38 return nil;
39 }
40
41 - (void)sayHello
42 {
43 NSLog(@"Hello world!");
44 }
45
46 @end