iOS 原型模式

  • 原型模式
    copy()、NSCopying協(xié)議
    數(shù)組、字典、集合中的元素也要可以復(fù)制,即實現(xiàn)NSCopy協(xié)議,否則崩潰
    數(shù)組、字典、集合注意需要深度拷貝copyItems:YES

  • 應(yīng)用,適用場景
    復(fù)制(深拷貝復(fù)雜對象)

拷貝協(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é)議實現(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ù)制的對象 子類不要重載
- (id)copyWithZone:(NSZone *)zone;

///復(fù)制(賦值操作) 由子類重載實現(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

班級類

//
//  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ù)制了集合里面的對象)
    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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容