The Singleton Pattern 單例模式 教程

1.ARC 單例模式

1.1ARC 單例模式

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
/** 名字 */
@property (nonatomic, strong) NSString *name;
@property (nonatomic, copy) NSString *city;
+ (instancetype)sharedPerson;
@end

Person.m

#import "Person.h"

@interface Person() <NSCopying>
@end

@implementation Person

static Person *_person;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _person = [super allocWithZone:zone];
    });
    return _person;
}

+ (instancetype)sharedPerson
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _person = [[self alloc] init];
    });
    return _person;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _person;
}
@end

ViewController.m

#import "ViewController.h"

#import "Person.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *person1 = [[Person alloc] init];
    person1.name = @"jack";
    NSMutableString *stringM = [NSMutableString stringWithFormat:@""];
    [stringM appendString:@"china"];
    [stringM appendString:@"beijing"];
    person1.city = stringM;
    
    Person *person2 = [[Person alloc] init];
    Person *person3 = [[Person alloc] init];
    Person *person4 = [[Person alloc] init];
    
    NSLog(@"%p %p %p %p", person1, person2, person3, person4);
    NSLog(@"%@ %@", person3.name,person3.city);
    
    NSLog(@"%@ %@", [Person sharedPerson], [Person sharedPerson]);
}

@end
ARC單粒模式[16876:372799] 0x7fb3f3f37e80 0x7fb3f3f37e80 0x7fb3f3f37e80 0x7fb3f3f37e80
ARC單粒模式[16876:372799] jack chinabeijing
ARC單粒模式[16876:372799] <Person: 0x7fb3f3f37e80> <Person: 0x7fb3f3f37e80>

1.2宏抽取 ARC 單例模式

Singleton.h

// .h文件
#define SingletonH + (instancetype)sharedInstance;

// .m文件
#define SingletonM \
static id _instace; \
 \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [super allocWithZone:zone]; \
    }); \
    return _instace; \
} \
 \
+ (instancetype)sharedInstance \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [[self alloc] init]; \
    }); \
    return _instace; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instace; \
}

Person.h

#import <Foundation/Foundation.h>

#import "Singleton.h"

@interface Person : NSObject
/** 名字 */
@property (nonatomic, strong) NSString *name;
@property (nonatomic, copy) NSString *city;

SingletonH
@end

Person.m

#import "Person.h"

@interface Person() <NSCopying>
@end

@implementation Person
SingletonM
@end
NSLog(@"%@ %@ %@ %@",[[Person alloc] init],
                     [Person new],
                     [[[Person alloc] init] copy], 
                     [Person sharedInstance]);

<Person: 0x7fc391e02f90> <Person: 0x7fc391e02f90> <Person: 0x7fc391e02f90> <Person: 0x7fc391e02f90>

1.3宏抽取 ARC 單例模式

Singleton.h

// .h文件
#define SingletonH(name) + (instancetype)shared##name;

// .m文件
#define SingletonM(name) \
static id _instance; \
 \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
 \
+ (instancetype)shared##name \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instance; \
}

Person.h

#import <Foundation/Foundation.h>
#import "Singleton.h"

@interface Person : NSObject
/** 名字 */
@property (nonatomic, strong) NSString *name;
@property (nonatomic, copy) NSString *city;

SingletonH(Person)
@end

Person.m

#import "Person.h"

@interface Person() <NSCopying>
@end

@implementation Person
SingletonM(Person)
@end

ViewController.h

#import <UIKit/UIKit.h>

#import "Singleton.h"

@interface ViewController : UIViewController
SingletonH(ViewController)
@end

ViewController.m

#import "ViewController.h"

#import "Person.h"

@implementation ViewController

SingletonM(ViewController)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@ %@ %@ %@", [[ViewController alloc] init],
                          [ViewController new],
                          [[[ViewController alloc] init] copy],
                          [ViewController sharedViewController]);
    
    NSLog(@"%@ %@ %@ %@",[[Person alloc] init],
                         [Person new],
                         [[[Person alloc] init] copy],
                         [Person sharedPerson]);

}

<ViewController: 0x7f897be9a120> <ViewController: 0x7f897be9a120> <ViewController: 0x7f897be9a120> <ViewController: 0x7f897be9a120>
<Person: 0x7f897bf19520> <Person: 0x7f897bf19520> <Person: 0x7f897bf19520> <Person: 0x7f897bf19520>

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容