Objective-C单例模式_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Objective-C单例模式

Objective-C单例模式

 2015/3/5 16:30:43  戴维营教育  程序员俱乐部  我要评论(0)
  • 摘要:单例类是一种特殊的类,在一个进程种只会存在一个该类的对象,在iOS应用中只会出现一个对象。这种设计模式在系统框架中许多地方都使用了,如NSFileManager、UIApplication等。在ARC的环境下,接口文件为:////DVISingleton.h////Copyright(c)2014长沙戴维营教育.Allrightsreserved.//#import<Foundation/Foundation.h>@interfaceDVISingleton:NSObject+
  • 标签:模式 单例模式

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

面试宝典1.0ver/images/singleton.jpg" alt=""></p>">class="alignnone size-full wp-image-495" src="/Upload/Images/2015030516/DAAD96AA8F77F84C.jpg" alt="singleton" width="528" height="174" />

    接口文件为:</li><br /> </ul>">
  • 在ARC的环境下,接口文件为:
Foundation/Foundation.h&amp;gt;

@interface DVISingleton : NSObject

+ (instancetype)sharedSingleton;

@end
&lt;/code&gt;&lt;/pre&gt;"><span class="hljs-comment">//
<span class="hljs-comment">//  DVISingleton.h
<span class="hljs-comment">//
<span class="hljs-comment">//  Copyright (c) 2014 长沙戴维营教育. All rights reserved.
<span class="hljs-comment">//

<span class="hljs-preprocessor">#import <span class="hljs-title">&lt;Foundation/Foundation.h&gt;

<span class="hljs-class"><span class="hljs-keyword">@interface <span class="hljs-title">DVISingleton : <span class="hljs-title">NSObject

+ (instancetype)sharedSingleton;

<span class="hljs-keyword">@end
</code></pre>">//
//  DVISingleton.h
//
//  Copyright (c) 2014 长沙戴维营教育. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DVISingleton : NSObject

+ (instancetype)sharedSingleton;

@end

实现文件:

线程安全,只允许执行依次
    static dispatch_once_t onceToken;
    dispatch_once(&amp;amp;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
&lt;/code&gt;&lt;/pre&gt;"><span class="hljs-comment">//
<span class="hljs-comment">//  DVISingleton.m
<span class="hljs-comment">//
<span class="hljs-comment">//  Copyright (c) 2014 长沙戴维营教育. All rights reserved.
<span class="hljs-comment">//

<span class="hljs-preprocessor">#import <span class="hljs-title">"DVISingleton.h"

<span class="hljs-class"><span class="hljs-keyword">@implementation <span class="hljs-title">DVISingleton

+ (instancetype)sharedSingleton
{
    <span class="hljs-keyword">static DVISingleton *sharedObject = <span class="hljs-literal">nil;

    <span class="hljs-comment">//线程安全,只允许执行依次
    <span class="hljs-keyword">static <span class="hljs-built_in">dispatch_once_t onceToken;
    <span class="hljs-built_in">dispatch_once(&amp;onceToken, ^{
        <span class="hljs-comment">//使用父类的allocWithZone:方法创建对象
        sharedObject = [[<span class="hljs-keyword">super allocWithZone:<span class="hljs-literal">NULL] init];
    });

    <span class="hljs-keyword">return sharedObject;
}

- (<span class="hljs-keyword">id)init
{
    <span class="hljs-keyword">if (<span class="hljs-keyword">self = [<span class="hljs-keyword">super init]) {

    }

    <span class="hljs-keyword">return <span class="hljs-keyword">self;
}

+ (<span class="hljs-keyword">id)allocWithZone:(<span class="hljs-keyword">struct _NSZone *)zone
{
    <span class="hljs-keyword">return [<span class="hljs-keyword">self sharedSingleton];
}

- (<span class="hljs-keyword">id)copy
{
    <span class="hljs-keyword">return <span class="hljs-keyword">self;
}

- (<span class="hljs-keyword">void)dealloc
{

}
<span class="hljs-keyword">@end
</code></pre>">//
//  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环境下的实现文件:
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
&lt;/code&gt;&lt;/pre&gt;"><span class="hljs-preprocessor">#import <span class="hljs-title">"DVISingleton.h"

<span class="hljs-class"><span class="hljs-keyword">@implementation <span class="hljs-title">DVISingleton

+ (instancetype)sharedSingleton
{
    <span class="hljs-keyword">static DVISingleton *sharedObject = <span class="hljs-literal">nil;

    <span class="hljs-comment">//线程安全,只允许执行依次
    <span class="hljs-keyword">static <span class="hljs-built_in">dispatch_once_t onceToken;
    <span class="hljs-built_in">dispatch_once(&amp;onceToken, ^{
        <span class="hljs-comment">//使用父类的allocWithZone:方法创建对象
        sharedObject = [[<span class="hljs-keyword">super allocWithZone:<span class="hljs-literal">NULL] init];
    });

    <span class="hljs-keyword">return sharedObject;
}

+ (<span class="hljs-keyword">id)allocWithZone:(NSZone *)zone {
  <span class="hljs-keyword">return [[<span class="hljs-keyword">self sharedSingleton] retain];
}
- (<span class="hljs-keyword">id)copyWithZone:(NSZone *)zone {
  <span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">id)retain {
  <span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">unsigned)retainCount {
  <span class="hljs-keyword">return UINT_MAX; <span class="hljs-comment">//denotes an object that cannot be released
}
- (oneway <span class="hljs-keyword">void)release {
  <span class="hljs-comment">// never release
}
- (<span class="hljs-keyword">id)autorelease {
  <span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">id)init {
  <span class="hljs-keyword">if (<span class="hljs-keyword">self = [<span class="hljs-keyword">super init]) {

  }
  <span class="hljs-keyword">return <span class="hljs-keyword">self;
}
- (<span class="hljs-keyword">void)dealloc {
  [<span class="hljs-keyword">super dealloc];
}
<span class="hljs-keyword">@end
</code></pre>">#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
发表评论
用户名: 匿名