zoukankan      html  css  js  c++  java
  • Objective-C辛格尔顿

    单例类是一种特殊的类。在一个进程种仅仅会存在一个该类的对象,在iOS应用中仅仅会出现一个对象。这样的设计模式在系统框架中很多地方都使用了。如NSFileManager、UIApplication等。

    singleton

    • 在ARC的环境下,接口文件为:
    //
    //  DVISingleton.h
    //
    //  Copyright (c) 2014 长沙戴维营教育. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface DVISingleton : NSObject
    
    + (instancetype)sharedSingleton;
    
    @end
    

    实现文件:

    //
    //  DVISingleton.m
    //
    //  Copyright (c) 2014 长沙戴维营教育. All rights reserved.
    //
    
    #import "DVISingleton.h"
    
    @implementation DVISingleton
    
    + (instancetype)sharedSingleton
    {
        static DVISingleton *sharedObject = nil;
    
        //线程安全。仅仅同意运行依次
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            //使用父类的allocWithZone:方法创建对象
            sharedObject = [[super allocWithZone:NULL] init];
        });
    
        return sharedObject;
    }
    
    - (id)init
    {
        if (self = [super init]) {
    
        }
    
        return self;
    }
    
    + (id)allocWithZone:(struct _NSZone *)zone
    {
        return [self sharedSingleton];
    }
    
    - (id)copy
    {
        return self;
    }
    
    - (void)dealloc
    {
    
    }
    @end
    
    • 在非ARC环境下的实现文件:
    <code class="objectivec hljs" data-origin="" <pre><code="" "dvisingleton.h""="" style="margin: 0px; padding: 0px; border: 0px; font-size: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(110, 107, 94); font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important;">#import "DVISingleton.h"
    
    @implementation DVISingleton
    
    + (instancetype)sharedSingleton
    {
        static DVISingleton *sharedObject = nil;
    
        //线程安全。仅仅同意运行依次
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            //使用父类的allocWithZone:方法创建对象
            sharedObject = [[super allocWithZone:NULL] init];
        });
    
        return sharedObject;
    }
    
    + (id)allocWithZone:(NSZone *)zone {
      return [[self sharedSingleton] retain];
    }
    - (id)copyWithZone:(NSZone *)zone {
      return self;
    }
    - (id)retain {
      return self;
    }
    - (unsigned)retainCount {
      return UINT_MAX; //denotes an object that cannot be released
    }
    - (oneway void)release {
      // never release
    }
    - (id)autorelease {
      return self;
    }
    - (id)init {
      if (self = [super init]) {
    
      }
      return self;
    }
    - (void)dealloc {
      [super dealloc];
    }
    @end

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    jQuery学习笔记3--网页字体变大变小
    jQuery学习笔记2--表格内容筛选
    jQuery学习笔记1--表格展开关系
    (转)PhoneGap开发环境搭建
    对HTML+CSS+JavaScript的个人理解
    (转)经典收藏 50个jQuery Mobile开发技巧集萃
    (转)phoneGap-Android开发环境搭建
    (转)面向移动设备的HTML5开发框架
    (转)前端攻略系列(二)
    (转)常见浏览器兼容性问题与解决技巧
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4852240.html
Copyright © 2011-2022 走看看