源码0501-10-掌握-单粒模式

单粒设计模式

单粒宏定义

//  ViewController.m
//  10-掌握-单粒模式
#import "ViewController.h"
#import "XMGPerson.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    XMGPerson *person1 = [[XMGPerson alloc] init];
//    person1.name = @"jack";
//    
//    XMGPerson *person2 = [[XMGPerson alloc] init];
//    XMGPerson *person3 = [[XMGPerson alloc] init];
//    XMGPerson *person4 = [[XMGPerson alloc] init];
    
//    NSLog(@"%p %p %p %p", person1, person2, person3, person4);
//    NSLog(@"%@", person3.name);
    
//    NSLog(@"%@ %@", [XMGPerson sharedPerson], [XMGPerson sharedPerson]);//两个类地址一样;
}

@end

//  XMGPerson.h
//  10-掌握-单粒模式
#import <Foundation/Foundation.h>

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

+ (instancetype)sharedPerson;
@end
//  XMGPerson.m
//  10-掌握-单粒模式
#import "XMGPerson.h"

@interface XMGPerson() <NSCopying>

@end

@implementation XMGPerson

static XMGPerson *_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];//self alloc 会自动掉用上面的allocWithZone方法
    });
    return _person;
}

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

 

//  XMGPerson.h
//  10-掌握-单粒模式
#import <Foundation/Foundation.h>

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

+ (instancetype)sharedPerson;
@end
//  XMGPerson.m
//  10-掌握-单粒模式
#import "XMGPerson.h"

@interface XMGPerson() <NSCopying>

@end

@implementation XMGPerson

static XMGPerson *_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

10-掌握-单粒模式

//  ViewController.h
//  10-掌握-单粒模式
#import <UIKit/UIKit.h>
#import "XMGSingleton.h"

@interface ViewController : UIViewController
XMGSingletonH
@end
//  ViewController.m
//  10-掌握-单粒模式

#import "ViewController.h"
#import "XMGPerson.h"
#import "XMGCar.h"

@interface ViewController ()

@end

@implementation ViewController

XMGSingletonM

- (void)viewDidLoad {
    [super viewDidLoad];
    

}

@end

单粒宏的定义

// .h文件
#define XMGSingletonH + (instancetype)sharedInstance;  //声明的地方

// .m文件
#define XMGSingletonM \  这里的一行替代后面的一片
static id _instance; \
 \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
 \
+ (instancetype)sharedInstance \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instace; \
}

 

//  XMGCar.h
//  10-掌握-单粒模式
#import <Foundation/Foundation.h>
#import "XMGSingleton.h"

@interface XMGCar : NSObject
XMGSingletonH
@end
//  XMGCar.m
//  10-掌握-单粒模式

#import "XMGCar.h"

@interface XMGCar()

@end

@implementation XMGCar
XMGSingletonM
@end

带参数的宏定义

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

// .m文件
#define XMGSingletonM(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; \
}

带参数的宏的使用

//  XMGPerson.h
//  10-掌握-单粒模式
#import <Foundation/Foundation.h>
#import "XMGSingleton.h"

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

XMGSingletonH(Person)
@end

//  XMGPerson.m
//  10-掌握-单粒模式
#import "XMGPerson.h"

@interface XMGPerson()

@end

@implementation XMGPerson
XMGSingletonM(Person)
@end
//  ViewController.m
//  10-掌握-单粒模式
#import "ViewController.h"
#import "XMGPerson.h"
#import "XMGCar.h"

@interface ViewController ()

@end

@implementation ViewController

XMGSingletonM(ViewController) 这之后 viewController就是一个单粒了

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@ %@", [ViewController sharedViewController], [[ViewController alloc] init]);
    NSLog(@"%@ %@", [XMGCar sharedCar], [[XMGCar alloc] init]);
    
//    XMGPerson *person1 = [[XMGPerson alloc] init];
//    person1.name = @"jack";
//    
//    XMGPerson *person2 = [[XMGPerson alloc] init];
//    XMGPerson *person3 = [[XMGPerson alloc] init];
//    XMGPerson *person4 = [[XMGPerson alloc] init];
    
//    NSLog(@"%p %p %p %p", person1, person2, person3, person4);
//    NSLog(@"%@", person3.name);
    
//    NSLog(@"%@ %@", [XMGPerson sharedPerson], [XMGPerson sharedPerson]);
}

@end

 

13-掌握-单粒模式(非GCD)

//  ViewController.m
//  10-掌握-单粒模式
#import "ViewController.h"
#import "XMGPerson.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    XMGPerson *person1 = [[XMGPerson alloc] init];
//    person1.name = @"jack";
//    
//    XMGPerson *person2 = [[XMGPerson alloc] init];
//    XMGPerson *person3 = [[XMGPerson alloc] init];
//    XMGPerson *person4 = [[XMGPerson alloc] init];
    
//    NSLog(@"%p %p %p %p", person1, person2, person3, person4);
//    NSLog(@"%@", person3.name);
    
    NSLog(@"%@ %@", [XMGPerson sharedInstance], [[XMGPerson alloc] init]);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{  //不加锁会存在同时掉用有安全问题;
        [[XMGPerson alloc] init];
    });
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[XMGPerson alloc] init];
    });
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[XMGPerson alloc] init];
    });
}

@end

 

//  XMGPerson.h
//  10-掌握-单粒模式
#import <Foundation/Foundation.h>

@interface XMGPerson : NSObject
+ (instancetype)sharedInstance;
@end
//  XMGPerson.m
//  10-掌握-单粒模式
#import "XMGPerson.h"

@interface XMGPerson()

@end

@implementation XMGPerson

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    @synchronized(self) { //加锁防撞
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    }
    return _instance;
}

+ (instancetype)sharedInstance
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    return _instance;
}

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

 

posted @ 2017-03-15 16:58  laugh  阅读(162)  评论(0编辑  收藏  举报