博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective c singleton design pattern
阅读量:6102 次
发布时间:2019-06-20

本文共 2132 字,大约阅读时间需要 7 分钟。

hot3.png

Creating a Singleton Instance

 

A singleton object acts as a kind of control center, directing or coordinating the services of the class. Your class should generate a singleton instance rather than multiple instances when there is conceptually only one instance (as with, for example, NSWorkspace). You use singleton instances rather than factory methods or functions when it is conceivable that there might be multiple instances one day.

To create a singleton as the sole allowable instance of a class in the current process, you need to have an implementation similar to the code below. This code does the following:

 Declare a static instance of your singleton object and initialize it to nil.

 

 In your class factory method for the class (named something like “sharedInstance” or “getInstance”),

generate an instance of the class but only if the static instance is nil.

 

 Override the allocWithZone: method to ensure that another instance is not allocated if someone tries to allocate and initialize an instance of your class directly instead of using the class factory method. Instead, just return the shared object.

 

 Implement the base protocol methods copyWithZone:, release, retain, retainCount, and autorelease to do the appropriate things to ensure singleton status. (The last four of these methods apply to memory-managed code, not to garbage-collected code.)

 

 

Creating a Singleton Instance

 

//

// MySingletonClass.m

// Created by Tracy E on 10-10-27.

// Copyright 2010 tracy.cpp  All rights reserved.

//

 

#import "MySingletonClass.h"

 

static MySingletonClass *singletonInstance = nil;

 

@implementation MySingletonClass

 

+ (id)getInstance {

     if (singletonInstance == nil)

     {

          singletonInstance = [[super allocWithZone:NULLinit];

     }

     return singletonInstance;

}

 

+ (id)allocWithZone:(NSZone *)zone

{

     return [[self getInstanceretain];

}

 

- (id)copyWithZone:(NSZone *)zone {

      return self;

}

- (id)retain {

      return self;

}

 

- (NSUInteger)retainCount {

       return NSUIntegerMax//denotes an object that cannot be released

}

 

- (void)release {

      //do nothing

}

 

- (id)autorelease {

      return self;

}

 

转载于:https://my.oschina.net/bankofchina/blog/271359

你可能感兴趣的文章
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
网卡驱动程序之框架(一)
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>
自己制作交叉编译工具链
查看>>
Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
查看>>
[物理学与PDEs]第3章习题1 只有一个非零分量的磁场
查看>>
深入浅出NodeJS——数据通信,NET模块运行机制
查看>>
onInterceptTouchEvent和onTouchEvent调用时序
查看>>
android防止内存溢出浅析
查看>>
4.3.3版本之引擎bug
查看>>
SQL Server表分区详解
查看>>
使用FMDB最新v2.3版本教程
查看>>
SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器
查看>>
STM32启动过程--启动文件--分析
查看>>
垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
查看>>