单例模式

2014-11-24 02:22:21 · 作者: · 浏览: 0

单例模式
Java代码
#import

@interface TCUtil : NSObject {

BOOL isOpenedOfPanel_;
}

@property (nonatomic, assign) BOOL isOpenedOfPanel;

// Singleton stuff
+ (TCUtil *)sharedUtilInstance;

@end

Java代码
//
// TCUtil.m
// TrendsCity
//
// Created by jiajun lv on 8/23/11.
// Copyright 2011 卓文华讯. All rights reserved.
//

#import "TCUtil.h"
#import

@implementation TCUtil

@synthesize isOpenedOfPanel = isOpenedOfPanel_;

#pragma mark -
#pragma mark Singleton stuff

static TCUtil *utilInstance_ = nil;

+ (TCUtil *)sharedUtilInstance {

if (!utilInstance_) {


utilInstance_ = [[self alloc] init];
}

return utilInstance_;
}

+ (id)alloc {

NSAssert(utilInstance_ == nil, @"Attempted to allocate a second instance of a singleton.");
return [super alloc];
}

- (id)init {

self = [super init];

if (self) {

isOpenedOfPanel_ = NO;
}

return self;
}

#pragma mark -
#pragma mark Memory management methods

- (void)dealloc {

[utilInstance_ release];
utilInstance_ = nil;

[super dealloc];
}

@end