[关闭]
@fiy-fish 2015-07-18T21:44:12.000000Z 字数 2230 阅读 1250

day07-04-单例

Objective-c


  1. // main.m
  2. // day07-04-单例
  3. //
  4. // Created by Aaron on 15/7/9.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. //单例
  9. //设计模式
  10. //一个类只会产生一个实例,而且不释放
  11. //这个实例叫做单例对象
  12. //这个类叫做单例类
  13. //单例的应用场景
  14. //废纸篓
  15. //网页计数器
  16. //单例的作用: 共享数据,用作管理
  17. #import "TaskManager.h"
  18. #import "TaskOne.h"
  19. #import "TaskTwo.h"
  20. void test()
  21. {
  22. TaskManager *manager = [TaskManager defaultManager];
  23. NSLog(@"%p",manager);
  24. //day07-04-单例[2128:174387] 0x10030fea0
  25. }
  26. int main(int argc, const char * argv[]) {
  27. @autoreleasepool {
  28. TaskManager *manager = [TaskManager defaultManager];
  29. NSLog(@"%p",manager);
  30. //day07-04-单例[2128:174387] 0x10030fea0
  31. TaskOne *one = [[TaskOne alloc] init];
  32. [one TaskRun];
  33. TaskTwo *two = [[TaskTwo alloc] init];
  34. [two TaskRun];
  35. TaskTwo *two1 = [[TaskTwo alloc] init];
  36. [two1 TaskRun];
  37. //manager = nil;
  38. [manager zhuxiao];
  39. NSLog(@"%@",manager.taskArray);
  40. //test();
  41. }
  42. return 0;
  43. }

  1. #import "Task.h"
  2. @interface TaskOne : Task
  3. @end

  1. #import "TaskOne.h"
  2. @implementation TaskOne
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. self.name = @"taskOne";
  8. }
  9. return self;
  10. }
  11. @end

  1. #import "Task.h"
  2. @interface TaskTwo : Task
  3. //-(void)TaskRun;
  4. @end

  1. #import "TaskTwo.h"
  2. #import "TaskManager.h"
  3. @implementation TaskTwo
  4. -(instancetype)init
  5. {
  6. if(self = [super init])
  7. {
  8. self.name = @"taskTwo";
  9. }
  10. return self;
  11. }
  12. -(void)TaskRun
  13. {
  14. TaskManager *manager = [TaskManager defaultManager];
  15. NSLog(@"TaskTwo.h %p",manager);
  16. //NSLog(@"%ld",manager.number);
  17. }
  18. @end

  1. #import <Foundation/Foundation.h>
  2. #import "Task.h"
  3. @interface TaskManager : NSObject
  4. @property (nonatomic,assign) NSInteger number;
  5. @property (nonatomic,retain) NSMutableArray *taskArray;
  6. //单例方法通常以default 或者share 开头
  7. //单例类需要用单例方法来创建对象
  8. +(instancetype)defaultManager;
  9. -(void)addTask:(Task *)task;
  10. -(void)zhuXiao;
  11. @end

  1. #import "TaskManager.h"
  2. @implementation TaskManager
  3. static TaskManager *manager = nil;
  4. +(instancetype)defaultManager
  5. {
  6. // static TaskManager *manager = nil;
  7. //加锁
  8. @synchronized(self)
  9. {
  10. if(!manager)
  11. {
  12. manager = [[self alloc] init];
  13. }
  14. }
  15. return manager;
  16. }
  17. -(void)zhuXiao
  18. {
  19. manager = nil;
  20. }
  21. -(void)addTask:(Task *)task
  22. {
  23. if(!_taskArray)
  24. {
  25. _taskArray = [NSMutableArray array];
  26. }
  27. [_taskArray addObject:task];
  28. }
  29. @end

  1. #import <Foundation/Foundation.h>
  2. @interface Task : NSObject
  3. @property (nonatomic,copy) NSString *name;
  4. -(void)TaskRun;
  5. @end

  1. #import "Task.h"
  2. #import "TaskManager.h"
  3. @implementation Task
  4. -(void)TaskRun
  5. {
  6. TaskManager *manager = [TaskManager defaultManager];
  7. NSLog(@"TaskOne.h %p",manager);
  8. manager.number = 1000;
  9. [manager addTask:self];
  10. }
  11. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注