zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    采用商业智能提升企业的数字营销策略
    采用商业智能提升企业的数字营销策略
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    2017 全球半导体预估跳增 11.5%,存储器最夯
    2017 全球半导体预估跳增 11.5%,存储器最夯
    如何从零学习PostgreSQL Page结构
    转成json必须是unicdoe字符
  • 原文地址:https://www.cnblogs.com/foxmin/p/2594201.html
Copyright © 2011-2022 走看看