iOS中的单实例的实现

我们知道在Cocoa Touch框架中有许多类是以单实例方式存在的,例如:UIApplication, NSUserDefault, UIDevice, NSFileManager等等; 当我们使用这些类的时候,我们得到的实例永远是同一个。今天我就将我不久之前实现的一个单实例类介绍给大家:

类名:Carrier (运营商)

作用:提供一些有关运营商的信息。

//
//  Carrier.h
//  Carrier Information
//
#import <Foundation/Foundation.h>

@interface Carrier : NSObject {
@private
    NSString *_carrierName;  // 运营商名称(中国移动、中国联通)
    NSString *_carrierCode; // 运营商国际编码(中国移动:46000,46002 中国联通:46001)
    NSString *_carrierServiceNumber; // 运营商服务热线(中国移动:10086 中国联通:10010)
}

@property (nonatomic, readonly) NSString *carrierName;
@property (nonatomic, readonly) NSString *carrierCode;
@property (nonatomic, readonly) NSString *carrierServiceNumber;

+ (id)currentCarrier;

@end


//
//  Carrier.m
//  Carrier Information
//

#import "Carrier.h"

#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

static Carrier *carrier = nil;

@implementation Carrier
@synthesize carrierName = _carrierName;
@synthesize carrierCode = _carrierCode;
@synthesize carrierServiceNumber = _carrierServiceNumber;

+ (id)currentCarrier {
    @synchronized(self) {
        if (carrier == nil) {
            carrier = [[Carrier alloc] init];
        }
    }    
    return carrier;
}

- (id)init {
    self = [super init];
    if (self) {
        CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
        _carrierName = [[NSString alloc] initWithString:info.subscriberCellularProvider.carrierName];
        NSMutableString *mutableString = [[NSMutableString alloc] initWithString:info.subscriberCellularProvider.mobileCountryCode];
        [mutableString appendString:info.subscriberCellularProvider.mobileNetworkCode];
        _carrierCode = mutableString;
        
        if ([mutableString isEqualToString:@"46001"]) {
            _carrierServiceNumber = [[NSString alloc] initWithString:@"10010"];
        } else if ([mutableString isEqualToString:@"46000"] || [_carrierCode isEqualToString:@"46002"]) {
            _carrierServiceNumber = [[NSString alloc] initWithString:@"10086"];
        } else {
            _carrierServiceNumber = [[NSString alloc] initWithString:NSLocalizedString(@"Unknow Carrier", @"")];
        }
        [info release];
    }
    
    return self;
}

+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self) {
        if (carrier == nil) {
            carrier = [super allocWithZone:zone];
            return carrier;
        }
    }
    return nil;
}

- (void)dealloc {
    [_carrierName release];
    [_carrierCode release];
    [_carrierServiceNumber release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (id)retain {
    return self;
}

- (oneway void)release {
    
}

- (id)autorelease {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

@end

iOS中的单利编程是十分必要的,它可以为我们节省很多系统资源,所以我们认为在必要的时候我们要使用单实例设计模式。

原文地址:https://www.cnblogs.com/OtionSky/p/iOS_Singleton.html