iOS设计模式之 Singleton(单例模式)

来源:互联网 发布:ssh框架留言板源码 编辑:程序博客网 时间:2024/05/23 22:26

【原文地址:http://jobleeimay.blog.163.com/blog/static/212532289201302473652537/】

单例模式就是只有一个实例,确保一个类只有一个实例,并且自行实例化并向整个系统提供这个实例,一个单例类可以实现在不同的窗口之间传递数据。

在oc中要实现一个单例类,至少需要做以下四个步骤:

1、为单例对象实现一个静态实例,并初始化,然后设置成nil,

2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,

3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实力的时候不产生一个新实例,

4、适当实现allocWitheZone,copyWithZone,release和autorelease


以下是一个简单的demo,实现了一个单例类向多个窗口传值。


#import <Foundation/Foundation.h>


@interface Singleton : NSObject

@property (nonatomic, retain) NSString *singletonValue;

//写一个单例

+(id) stirngManager;


@end

#import "Singleton.h"

static Singleton *singleton;

@implementation Singleton

@synthesize singletonValue;


BOOL isFromSelf = NO;


//将单例实现

+(id)stirngManager

{

    if (singleton == nil) {

        isFromSelf = YES;

        singleton = [[Singleton alloc]init];

        isFromSelf = NO;

    }

    return singleton;

}

// 2. 单例在什么时候都不要释放 因为其实就是全局变量


+ (id) alloc { // 3. 如何避免单例被alloc

    if (isFromSelf ) {

        return [super alloc];

    } else {

        return nil;

    }

}

- (void) dealloc {

    NSLog(@"in dealloc");

    // ARC 不用在写[super dealloc]

    // ARC 不能用autorelease 系统会自动加autorelease

    //    [super dealloc];

}

@end




#import <UIKit/UIKit.h>

#import "Singleton.h"



#import "Receiver0.h"

@interface Poster : UIViewController


@end


#import "Poster.h"


@interface Poster ()


@end


@implementation Poster


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"V2 Controller";

    

    UIButton *redButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    redButton.frame = CGRectMake(100, 20, 100, 40);

    [redButton setTitle:@"第一次单例传值" forState:UIControlStateNormal];

    [redButton addTarget:self action:@selector(buttonClick1:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:redButton];

    

    

    UIButton *blackButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    blackButton.frame = CGRectMake(100, 80, 100, 40);

    [blackButton setTitle:@"第二次单例传值" forState:UIControlStateNormal];

    [blackButton addTarget:self action:@selector(buttonClick2:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:blackButton];

}

- (void) buttonClick1:(id)arg {//-------发广播


    Singleton *sin = [Singleton stirngManager];

    sin.singletonValue = @"单例传值1";

    Receiver0 *receiver0 = [[Receiver0 alloc] init];

    [self.navigationController pushViewController:receiver0 animated:YES];

}

- (void) buttonClick2:(id)arg {//-------发广播

    

    Singleton *sin = [Singleton stirngManager];

    sin.singletonValue = @"单例传值2";

    Receiver0 *receiver0 = [[Receiver0 alloc] init];

    [self.navigationController pushViewController:receiver0 animated:YES];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




#import <UIKit/UIKit.h>

#import "Receiver1.h"

@interface Receiver0 : UIViewController


@end


#import "Receiver0.h"

@interface Receiver0 ()


@end


@implementation Receiver0


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"Receiver0 Controller";

    

    UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    b.frame = CGRectMake(100, 50, 100, 40);

    [b addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchDown];

    [b setTitle:@"Receiver1" forState:UIControlStateNormal];

    [self.view addSubview:b];

    

    Singleton *sin = [Singleton stirngManager];

    UILabel *showValueLabel = [[UILabelalloc]initWithFrame:CGRectMake(0, 100, 320, 360)];

    showValueLabel.textAlignment = NSTextAlignmentCenter;

    showValueLabel.backgroundColor = [UIColor grayColor];

    showValueLabel.text = sin.singletonValue;

    [self.view addSubview:showValueLabel];

}

- (void) buttonClick:(id)arg {

    Receiver1 *receiver1 = [[Receiver1 alloc] init];

    [self.navigationController pushViewController:receiver1 animated:YES];

    //[v1 release];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end





#import <UIKit/UIKit.h>

#import "Poster.h"

@interface Receiver1 : UIViewController


@end



#import "Receiver1.h"


@interface Receiver1 ()


@end


@implementation Receiver1


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"V1 Controller";

    

    UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    b.frame = CGRectMake(100, 50, 100, 40);

    [b addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchDown];

    [b setTitle:@"Poster" forState:UIControlStateNormal];

    [self.view addSubview:b];

    

    Singleton *sin = [Singleton stirngManager];

    UILabel *showValueLabel = [[UILabelalloc]initWithFrame:CGRectMake(0, 100, 320, 360)];

    showValueLabel.textAlignment = NSTextAlignmentCenter;

    showValueLabel.backgroundColor = [UIColor grayColor];

    showValueLabel.text = sin.singletonValue;

    [self.view addSubview:showValueLabel];

}

- (void) buttonClick:(id)arg {

    Poster *poster = [[Poster alloc] init];

    [self.navigationController pushViewController:poster animated:YES];

    //[v1 release];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0
原创粉丝点击