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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容