年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2303|回复: 2

第二十七讲:Objective-C设计模式之单例

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-2-1 21:38:54 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-2-1 21:40 编辑

    单例,即全局变量
    ThemeManager.h
    1. //
    2. //  ThemeManager.h
    3. //  ThemeManager
    4. //
    5. //  Created by yusian on 14-2-1.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface ThemeManager : NSObject
    10. @property (nonatomic, retain) NSString *name;
    11. + (id) sharedThemeManager;
    12. @end
    复制代码
    ThemeManager.m
    1. //
    2. //  ThemeManager.m
    3. //  ThemeManager
    4. //
    5. //  Created by yusian on 14-2-1.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "ThemeManager.h"
    9. @implementation ThemeManager
    10. static ThemeManager *s;
    11. + (id) sharedThemeManager {
    12.     if (s == nil) {
    13.         s = [[[self class] alloc] init];
    14.     }
    15.     return s;
    16. }
    17. - (id) init {
    18.     if (self = [super init]) {
    19.         self.name = [NSString stringWithFormat:@"Default"];
    20.     }
    21.     return self;
    22. }
    23. - (NSString *) description {
    24.     return [NSString stringWithFormat:@"My name is %@",self.name];
    25. }
    26. - (void) dealloc {
    27.     [_name release];
    28.     _name = Nil;
    29.     [super dealloc];
    30. }
    31. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  ThemeManager
    4. //
    5. //  Created by yusian on 14-2-1.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "ThemeManager.h"
    10. int main(int argc, const char * argv[])
    11. {
    12.     @autoreleasepool {
    13.         
    14.         ThemeManager * tm = [ThemeManager sharedThemeManager];
    15.         
    16.         ThemeManager * tm2 = [ThemeManager sharedThemeManager];
    17.                         
    18.         NSLog(@"tm is %@",tm);
    19.         NSLog(@"tm2 is %@",tm2);
    20.         
    21.         NSLog(@"tm address is  %p, tm2 address is %p",tm,tm2);
    22.     }
    23.     return 0;
    24. }
    复制代码
    运行结果:
    1. 2014-02-01 21:40:05.406 ThemeManager[7216:303] tm is My name is Default
    2. 2014-02-01 21:40:05.408 ThemeManager[7216:303] tm2 is My name is Default
    3. 2014-02-01 21:40:05.408 ThemeManager[7216:303] tm address is  0x100301c00, tm2 address is 0x100301c00
    4. Program ended with exit code: 0
    复制代码
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-2-1 22:01:50 | 显示全部楼层
    典型的单例的写法:
    + (id) sharedThemeManager {
            if (s == nil) {
                s = [[[self class] alloc] init];
            }

        return s;
    }
    加锁的写法:
    + (id) sharedThemeManager {
        @synchronized(self){
        if (s == nil) {
            s = [[[self class] alloc] init];
        }
        }
        return s;
    }

    GCD的写法:
    + (id) sharedThemeManager {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
        if (s == nil) {
            s = [[[self class] alloc] init];
        }
        });
        return s;
    }



  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-2-2 11:26:30 | 显示全部楼层
    重载的几个方法写法:
    + (id) copyWithZone: (NSZone *)zone {
        return self;
    }

    - (id) retain {
        return self;
    }

    - (oneway void) release {

    }

    - (NSUInteger) retainCount {
        return UINT_MAX;
    }

    - (id) autorelease {
        return self;
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-2 09:17 , Processed in 0.043450 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表