单例模式,就是我们定义的⼀个类,这个类只创建⼀次对象,也只创建⼀个对象。这个类的对象,供整个程序使⽤。
单例模式的三个必要条件:
1、单例类只能有⼀个对象
2、这个变量必须是⾃⼰⾃⾏创建的。
3、这个变量必须给整个程序使⽤
// main.m
// Single
// Created by cqy on 15/12/26.
// Copyright © 2015年 程清杨. All rights reserved.
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [Student share];
NSLog(@"student = %@",student);
student.name = @"Qing Yang";
Student *stu1 = [Student share];
NSLog(@"stu1 = %@",stu1);
Student *stu2 = [[Student alloc] init];
NSLog(@"stu2 = %@",stu2);
Student *stu3 = [stu2 copy];
NSLog(@"stu3 = %@",stu3);
Student *stu4 = [stu2 mutableCopy];
NSLog(@"stu4 = %@",stu4);
}
return 0;
// Copyright © 2015年 程清杨. All rights reserved.
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [Student share];
NSLog(@"student = %@",student);
student.name = @"Qing Yang";
Student *stu1 = [Student share];
NSLog(@"stu1 = %@",stu1);
Student *stu2 = [[Student alloc] init];
NSLog(@"stu2 = %@",stu2);
Student *stu3 = [stu2 copy];
NSLog(@"stu3 = %@",stu3);
Student *stu4 = [stu2 mutableCopy];
NSLog(@"stu4 = %@",stu4);
}
return 0;
}
// Student.h
// Single
// Created by cqy on 15/12/26.
// Copyright © 2015年 程清杨. All rights reserved.
//单例的实现:
//1、在.h文件中声明一个类方法,用于初始化实例
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCopying,NSMutableCopying>
@property(nonatomic,copy)NSString *name;
+(id)share;
// Single
// Created by cqy on 15/12/26.
// Copyright © 2015年 程清杨. All rights reserved.
//单例的实现:
//1、在.h文件中声明一个类方法,用于初始化实例
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCopying,NSMutableCopying>
@property(nonatomic,copy)NSString *name;
+(id)share;
@end
// Student.m
// Single
// Created by cqy on 15/12/26.
// Copyright © 2015年 程清杨. All rights reserved.
#import "Student.h"
//在.m中,首先声明一个static修饰的对象,此对象此时是一个空指针
static Student *stu = nil;
@implementation Student
//3、实现.h中声明的类方法
+(id)share{
//避免多线程操作带来的弊端
@synchronized(self) {
if (stu == nil){stu = [[Student alloc] init];}
}
return stu;
}
//4、避免alloc产生新对象,所以需要重写allocWithZone方法
+(id)allocWithZone:(struct _NSZone *)zone{
if ( stu == nil){
stu = [super allocWithZone:zone];
}
return stu;
}
//5、为了避免拷贝产生新对象,我们需要重写copywithzone以及mutablecopywithzone方法
-(id)copyWithZone:(NSZone *)zone{
return stu;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return stu;
// Single
// Created by cqy on 15/12/26.
// Copyright © 2015年 程清杨. All rights reserved.
#import "Student.h"
//在.m中,首先声明一个static修饰的对象,此对象此时是一个空指针
static Student *stu = nil;
@implementation Student
//3、实现.h中声明的类方法
+(id)share{
//避免多线程操作带来的弊端
@synchronized(self) {
if (stu == nil){stu = [[Student alloc] init];}
}
return stu;
}
//4、避免alloc产生新对象,所以需要重写allocWithZone方法
+(id)allocWithZone:(struct _NSZone *)zone{
if ( stu == nil){
stu = [super allocWithZone:zone];
}
return stu;
}
//5、为了避免拷贝产生新对象,我们需要重写copywithzone以及mutablecopywithzone方法
-(id)copyWithZone:(NSZone *)zone{
return stu;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return stu;
}
@end