iOS--单例模式

单例模式的概念

单例模式是一种设计模式,旨在确保某个类只有一个实例,并提供全局访问点以供其他对象使用。它在许多软件应用中被广泛使用,以便在系统中共享一个唯一的实例。

单例模式的优点

1.单例模式提供了一个全局访问点,使得其他对象可以方便地获取单例类的实例
2.单例模式确保一个类只有一个实例存在,这可以节省系统资源,避免了多个实例之间的冲突和不一致性。
3.单例模式可以用于管理共享的状态或数据,使得多个对象可以共享同一份数据,避免数据不一致的问题。(也是第二点的特点)
4.延迟实例化:单例模式允许在需要的时候才创建实例,避免了不必要的资源消耗。(这里不是绝对的,所以我们将单例模式还可细分为懒汉模式和饿汉模式,一个省空间,一个省时间,下面会讲)

单例模式的缺点

1.从代码原则上来讲,单例模式其实是有问题的,但oc语言是针对对象开发的语言,单例模式任然发挥着不可替代的作用 ;所以,即便单例模式会破坏单一职责原则,引入全局状态,导致代码复杂性的增加和并发和线程安全的问题,我们也离不开这种模式 ;

2.由于单例类的实例是全局共享的,对单例类的修改可能会影响到系统的其他部分,使得扩展和维护变得困难。

单例模式如何实现

简单的模式

#import "singleton.h"
static id sharedinstance ;

@implementation singleton

+ (id)sharedinstance {
    if (sharedinstance == nil) {
        sharedinstance = [[self alloc]init] ;
    }
    return sharedinstance;
}
@end

加锁

#import "singleton.h"
static id sharedinstance ;

@implementation singleton

+ (id)sharedinstance {
    if (sharedinstance == nil) {
        @synchronized (self) {
            if (sharedinstance == nil) {
                sharedinstance = [[self alloc]init] ;
            }
            return sharedinstance;
        }
    }
    return sharedinstance;
}
@end

CDK模式

CDK和锁一样都是后眠药学习的内容,就先没写了,之后补上 ;

常用模式

为了确保单例模式中单例的唯一性,以下形式更为准确使用

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface singleton : NSObject <NSCopying,NSMutableCopying>
+ (id)sharedinstance ;
@end

NS_ASSUME_NONNULL_END
#import "singleton.h"
static id _sharedinstance ;

@implementation singleton

+ (instancetype) allocWithZone:(struct _NSZone *)zone {
    if (_sharedinstance == nil) {
        @synchronized (self) {
            if (_sharedinstance == nil) {
                _sharedinstance = [super allocWithZone:zone ] ;
            }
        }
        
    }
    return _sharedinstance;
}

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

- (id)copyWithZone:(NSZone *)zone {
    return _sharedinstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _sharedinstance;
}

@end

其外解释一下懒汉模式和饿汉模式

懒汉模式

道理类似于懒加载,就是在要使用该单例时在创建该单例对象,可以控制单例对象产生的时间,但也意味着要执行类方法来创建对象,会多耗费些许时间
上面常用方法中的就是懒汉模式;

饿汉模式

在该类创建就创建一个对象存储在静态区,允许全局调用 ;

#import "singleton.h"
static id _sharedinstance ;

@implementation singleton

+ (instancetype) allocWithZone:(struct _NSZone *)zone {
    if (_sharedinstance == nil) {
        @synchronized (self) {
            if (_sharedinstance == nil) {
                _sharedinstance = [super allocWithZone:zone ] ;
            }
        }
        
    }
    return _sharedinstance;
}

+ (void) load {
    _sharedinstance = [[self alloc] init] ;
}//类创建时就会执行

//+ (instancetype) sharedinstance {
//    if (_sharedinstance == nil) {
//        @synchronized (self) {
//            if (_sharedinstance == nil) {
//                _sharedinstance = [[self alloc]init] ;
//            }
//            return _sharedinstance;
//        }
//    }
//    return _sharedinstance;
//}

- (id)copyWithZone:(NSZone *)zone {
    return _sharedinstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _sharedinstance;
}

@end

无论是哪种模式,单例对象都存储在静态区,只有在程序结束时才会销毁 ;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值