Cocoa设计模式之单例

在objective-c中要实现一个单例类,至少需要做以下四个步骤:
1、为单例对象实现一个静态实例,并初始化,然后设置成nil,

static MTNetworkEnvironment *g_instance = nil;

2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,

+ (MTNetworkEnvironment *)sharedInstance
{
    @synchronized(self) {
        if ( g_instance == nil ) {
            g_instance = [[self alloc] init];
        }
    }
    return g_instance;
}

3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实力的时候不产生一个新实例,

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

这里的锁适用于防止多线程,保证该操作只在一个线程中执行。

4、适当实现allocWitheZone,copyWithZone,release和autorelease。

头文件:

/**
 * @file         MTNetworkEnvironment.h 
 * @brief       MoneyTree network environment
 * @author      philiphu@mintcode.com
 * @date          2012-07-14
 * @version      1.0.0 
 */

#import <Foundation/Foundation.h>
#import "Reachability.h"

/**
 * @brief           create and manage network enviroment
 */
@interface MTNetworkEnvironment : NSObject

/**
 * @brief           get the signalton engine object
 * @return          the engine object 
 */
+ (MTNetworkEnvironment *)sharedInstance;

/**
 * @brief           get the network statue 
 */
- (BOOL)isNetworkReachable;

/**
 * @brief           Judgment wifi is connected
 */
- (BOOL)isEnableWIFI;

/**
 * @brief           To judge whether the 3G connection
 */
- (BOOL)isEnable3G;
@end

实现文件:

/**
 * @file         MTNetworkEnvironment.m 
 * @brief       MoneyTree network environment
 * @author      philiphu@mintcode.com
 * @date          2012-07-14
 * @version      1.0.0 
 */

#import "MTNetworkEnvironment.h"

@interface MTNetworkEnvironment(Private)

@end


@implementation MTNetworkEnvironment


static MTNetworkEnvironment *g_instance = nil;


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

- (id)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


/**
 * @brief           get the signalton engine object
 * @return          the engine object 
 */
+ (MTNetworkEnvironment *)sharedInstance
{
    @synchronized(self) {
        if ( g_instance == nil ) {
            g_instance = [[self alloc] init];
        }
    }
    return g_instance;
}


/**
 * @brief           get the network statue 
 */
- (BOOL)isNetworkReachable
{
    BOOL isReachable = NO;
    Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reachability currentReachabilityStatus]) {
        case NotReachable:{
            isReachable = NO;
        }
            break;
        case ReachableViaWWAN:{
            isReachable = YES;
        }
            break;
        case ReachableViaWiFi:{
            isReachable = YES;   
        }
            break;
        default:
            isReachable = NO;
            break;
    }
    return isReachable;
}

/**
 * @brief           Judgment wifi is connected
 */
- (BOOL)isEnableWIFI
{
     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

/**
 * @brief           To judge whether the 3G connection
 */
- (BOOL)isEnable3G
{
    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}


@end

 

 

posted @ 2012-07-16 21:29  FoxBabe  阅读(266)  评论(0编辑  收藏  举报