iOS 原型模式

  • 原型模式
    copy()、NSCopying协议
    数组、字典、集合中的元素也要可以复制,即实现NSCopy协议,否则崩溃
    数组、字典、集合注意需要深度拷贝copyItems:YES

  • 应用,适用场景
    复制(深拷贝复杂对象)

拷贝协议

//
//  PrototypeCopyProtocol.h
//  LearnPrototype
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 

@protocol PrototypeCopyProtocol 

///复制自己
- (id)clone;

@end

学生数据模型类(遵守拷贝协议)

//
//  StudentModel.h
//  LearnPrototype
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 
#import "PrototypeCopyProtocol.h"

@interface StudentModel : NSObject

@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];
    ///完成复杂操作的所有作业
    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];
    ///学生1
    StudentModel *studnet1 = [[StudentModel alloc] init];
    studnet1.name = @"yinlinqvan";
    studnet1.age = @(26);
    studnet1.address = @"上海";
    studnet1.totalScore = @(100);
    ///学生2
    StudentModel *studnet2 = [studnet1 clone];
    studnet2.name = @"linda";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

NSCoping协议实现

//
//  BaseCopyObject.h
//  LearnNSCopying
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 

@interface BaseCopyObject : NSObject

///复制的对象 子类不要重载
- (id)copyWithZone:(NSZone *)zone;

///复制(赋值操作) 由子类重载实现
- (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];
    ///赋值操作作业
    [self copyOperationWithObject:copyObject];
    return copyObject;
}

- (void)copyOperationWithObject:(id)object {
    
}

@end

学生类

//
//  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

班级类

//
//  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;
    // 完成了深拷贝(完整的复制了集合里面的对象)
    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 = @"班级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

你可能感兴趣的:(iOS 原型模式)