zoukankan      html  css  js  c++  java
  • IOS单例的设计模式

    static SurveyRunTimeData *sharedObj = nil; //第一步:静态实例,并初始化置为nil。

    @implementation SurveyRunTimeData

    + (SurveyRunTimeData*) sharedInstance    //第二步:实例构造检查静态实例是否为nil。

    {    

    @synchronized (self)     {

            if (sharedObj == nil)

            {            

          [[self alloc] init];

            }    

    }

        return sharedObj;

    }

    + (id) allocWithZone:(NSZone *)zone         //第三步:重写allocWithZone方法 {

        @synchronized (self) {        

      if (sharedObj == nil) {

                sharedObj = [super allocWithZone:zone];

                return sharedObj;

            }

        }

        return nil;

    }

    - (id) copyWithZone:(NSZone *)zone 

            //第四步

    {    

    return self;

    }

    - (id) retain {

        return self;

    }

    - (unsigned) retainCount {

        return UINT_MAX;

    }

    - (oneway void) release {   

      }

    - (id) autorelease {

        return self;

    }

    - (id)init {

        @synchronized(self) {

            [super init];            

    //往往放一些要初始化的变量.        

    return self;    

    }

    }

    @end

  • 相关阅读:
    Docker学习总结(一)--Docker简介
    Liunx软件安装之Zabbix监控软件
    Liunx软件安装之Nginx
    Liunx软件安装之Redis
    Liunx软件安装之Tomcat
    Liunx软件安装之JDK
    Liunx软件安装之MySQL
    Liunx学习总结(九)--防火墙
    tensorflow 错误
    anaconda安装失败
  • 原文地址:https://www.cnblogs.com/zhibin/p/4118120.html
Copyright © 2011-2022 走看看