zoukankan      html  css  js  c++  java
  • 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中的单利编程是十分必要的,它可以为我们节省很多系统资源,所以我们认为在必要的时候我们要使用单实例设计模式。

  • 相关阅读:
    机器学习:不平衡信息有序平均加权最近邻算法IFROWANN
    项目代码管理工具Git的总结
    jquery的返回顶端的功能实现
    web项目中登陆超时的功能实现(基于C#)
    存储过程:项目中使用存储过程的一个实例
    Vbox中unbuntu15.10与win10共享文件 及开启复制粘贴功能
    网站开发常用Sql语句
    维护基于ASP.NET的网站的学习-SqlCommand类介绍及存储过程
    本地向服务器上传文件的方式-FTP工具上传
    本地向服务器上传文件的方式-本地资源映射到服务器
  • 原文地址:https://www.cnblogs.com/OtionSky/p/iOS_Singleton.html
Copyright © 2011-2022 走看看