原型模式
copy()锰茉、NSCopying協(xié)議
數(shù)組呢蔫、字典、集合中的元素也要可以復(fù)制飒筑,即實(shí)現(xiàn)NSCopy協(xié)議片吊,否則崩潰
數(shù)組绽昏、字典、集合注意需要深度拷貝copyItems:YES應(yīng)用俏脊,適用場(chǎng)景
復(fù)制(深拷貝復(fù)雜對(duì)象)
拷貝協(xié)議
//
// PrototypeCopyProtocol.h
// LearnPrototype
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol PrototypeCopyProtocol <NSObject>
///復(fù)制自己
- (id)clone;
@end
學(xué)生數(shù)據(jù)模型類(遵守拷貝協(xié)議)
//
// StudentModel.h
// LearnPrototype
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PrototypeCopyProtocol.h"
@interface StudentModel : NSObject<PrototypeCopyProtocol>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, strong) NSString *address;
@property (nonatomic, strong) NSNumber *totalScore;
- (id)clone;
@end
//
// StudentModel.m
// LearnPrototype
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "StudentModel.h"
@implementation StudentModel
- (id)clone {
StudentModel *student = [[[self class] alloc] init];
///完成復(fù)雜操作的所有作業(yè)
student.name = self.name;
student.age = self.age;
student.address = self.address;
student.totalScore = self.totalScore;
return student;
}
@end
使用
//
// ViewController.m
// LearnPrototype
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
///學(xué)生1
StudentModel *studnet1 = [[StudentModel alloc] init];
studnet1.name = @"yinlinqvan";
studnet1.age = @(26);
studnet1.address = @"上海";
studnet1.totalScore = @(100);
///學(xué)生2
StudentModel *studnet2 = [studnet1 clone];
studnet2.name = @"linda";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NSCoping協(xié)議實(shí)現(xiàn)
//
// BaseCopyObject.h
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BaseCopyObject : NSObject<NSCopying>
///復(fù)制的對(duì)象 子類不要重載
- (id)copyWithZone:(NSZone *)zone;
///復(fù)制(賦值操作) 由子類重載實(shí)現(xiàn)
- (void)copyOperationWithObject:(id)object;
@end
//
// BaseCopyObject.m
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "BaseCopyObject.h"
@implementation BaseCopyObject
- (id)copyWithZone:(NSZone *)zone {
BaseCopyObject *copyObject = [[self class] allocWithZone:zone];
///賦值操作作業(yè)
[self copyOperationWithObject:copyObject];
return copyObject;
}
- (void)copyOperationWithObject:(id)object {
}
@end
學(xué)生類
//
// StudentModel.h
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "BaseCopyObject.h"
@interface StudentModel : BaseCopyObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *age;
@end
//
// StudentModel.m
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "StudentModel.h"
@implementation StudentModel
- (void)copyOperationWithObject:(StudentModel *)object {
object.name = self.name;
object.age = self.age;
}
@end
班級(jí)類
//
// ClassModel.h
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "BaseCopyObject.h"
@interface ClassModel : BaseCopyObject
@property (nonatomic, strong) NSString *className;
@property (nonatomic, strong) NSArray *students;
@end
//
// ClassModel.m
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "ClassModel.h"
@implementation ClassModel
- (void)copyOperationWithObject:(ClassModel *)object {
object.className = self.className;
// 完成了深拷貝(完整的復(fù)制了集合里面的對(duì)象)
object.students = [[NSArray alloc] initWithArray:self.students copyItems:YES];
}
@end
使用
//
// ViewController.m
// LearnNSCopying
//
// Created by 印林泉 on 2017/3/5.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "ViewController.h"
#import "StudentModel.h"
#import "ClassModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StudentModel *student1 = [[StudentModel alloc] init];
student1.name = @"yinlinqvan";
StudentModel *student2 = student1.copy;
ClassModel *class1 = [[ClassModel alloc] init];
class1.className = @"班級(jí)1";
class1.students = @[student1, student2];
ClassModel *class2 = class1.copy;
NSLog(@"%@ %@", class1, class2);
NSLog(@"%@", class1.students);
NSLog(@"%@", class2.students);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end