iOS中的单实例的实现
2011-11-17 16:06
155 查看
我们知道在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中的单利编程是十分必要的,它可以为我们节省很多系统资源,所以我们认为在必要的时候我们要使用单实例设计模式。
类名: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中的单利编程是十分必要的,它可以为我们节省很多系统资源,所以我们认为在必要的时候我们要使用单实例设计模式。
相关文章推荐
- iOS开发中实现一个简单的图片浏览器的实例讲解
- iOS中UIWebView的使用详解及利用webview实现图文混排实例
- iOS 用自签名证书实现 HTTPS 请求的原理实例讲解
- ios弹幕高效加载实现方式实例代码
- iOS利用UIScrollView实现图片的缩放实例代码
- iOS实现的多条折线图封装实例
- iOS模仿QQ侧边栏的实现方法实例
- iOS实现第三方微信登录方式实例解析(最新最全)
- iOS 用自签名证书实现 HTTPS 请求的原理实例讲解
- Android 实现IOS 滚轮选择控件的实例(源码下载)
- iOS的动态创建实例方法和实现消息转发
- iOS---仿京东分类菜单实例实现
- vue.js实现仿原生ios时间选择组件实例代码
- iOS实现输入框跟随键盘自动上移的实例代码
- iOS实现翻页效果动画实例代码
- iOS实现搭建聊天页面的实例代码
- IOS 文本高度自适应变化实现实例
- iOS之UI--使用SWRevealViewController实现侧边菜单功能详解实例
- iOS实现视频压缩上传实例代码
- 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例