iOS_45_单例模式

音乐 视频播放控制器 应该做成单例模式,

当关闭播放界面时,音乐并不停止;

当重新进入播放界面时,还是原来的歌词,还是熟悉的韵味

如图所示:



为此:使用Singleton.pch文件

//
//  Singleton.pch
//  15单例模式
//
//  Created by beyond on 2018/1/2.
//  Copyright © 2018年 beyond. All rights reserved.
//

#ifndef Singleton_pch
#define Singleton_pch

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.


#define singleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)

#define singleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
#else
#define singleM static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shareTools\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
#endif




#endif /* Singleton_pch */




控制器.h,只需要:

//
//  ThirdViewCtrl.h
//  15单例模式
//
//  Created by beyond on 2018/1/2.
//  Copyright © 2018年 beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThirdViewCtrl : UIViewController
singleH(ThirdViewCtrl)
 
@end

控制器.m,只需要

//
//  ThirdViewCtrl.m
//  15单例模式
//
//  Created by beyond on 2018/1/2.
//  Copyright © 2018年 beyond. All rights reserved.
//

#import "ThirdViewCtrl.h"

@interface ThirdViewCtrl ()
@end

@implementation ThirdViewCtrl
singleM(ThirdViewCtrl)
- (void)viewDidLoad {
    [super viewDidLoad];

}

-(void)dealloc
{
    NSLog(@"sg__音乐播放器VC__dealloc");
}
 
@end



附PCH文件创建和配置方法:

1. 在Supporting Files下面,右键 New File, 选择 Other 里面的PCH File

2. 点击相应的TARGETS --->Build Setting ,搜索Prefix Header

3. 将Precompile Prefix Header 勾选为YES

4. 将左上角 项目的名字拷贝一下,  双击Prefix Header一栏, 填入:【项目的名字/Beyond.pch





附:ARC环境下,单例的思想


app运行期间,共享一份资源(这份资源只需要创建初始化1次),一般用于工具类。例如:登陆控制器,网络数据请求,音乐播放器等一个工程需要使用多次的控制器或方法。


单例模式的特点

优点:

单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。

如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

单例模式因为类控制了实例化过程,所以类可以更加灵活修改实例化过程。


缺点:

单例对象一旦建立,对象指针是保存在静态区的,单例对象在堆中分配的内存空间,会在应用程序终止后才会被释放。

单例类无法继承,因此很难进行类的扩展。

单例不适用于变化的对象,如果同一类型的对象总是要在不同的用例场景发生变化,单例就会引起数据的错误,不能保存彼此的状态。


ARC中单例实现步骤

1 在类的内部提供一个static修饰的全局变量
2 提供一个类方法,方便外界访问
3 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
4 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法

ARC中单例代码实现

#import "Tools.h"

@implementation Tools
// 创建静态对象 防止外部访问
static Tools *_instance;
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    //    @synchronized (self) {
    //        // 为了防止多线程同时访问对象,造成多次分配内存空间,所以要加上线程锁
    //        if (_instance == nil) {
    //            _instance = [super allocWithZone:zone];
    //        }
    //        return _instance;
    //    }
    
    
    // 也可以使用一次性代码
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}


// 为了使实例易于外界访问 我们一般提供一个类方法
// 类方法命名规范 share类名|default类名|类名
+(instancetype)shareTools
{
    //return _instance;
    // 最好用self 用Tools他的子类调用时会出现错误
    return [[self alloc]init];
}


// 为了严谨,也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
    return _instance;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值