iOS原型模式

原型模式:大部分重复,只有一小部分不同的情况下,为了代码清晰和避免麻烦,用原型模式,会更方便一点

    // 学生1
    StudentModel *stu1 = [[StudentModel alloc] init];
    stu1.name          = @"小王";
    stu1.age           = @(19);
    stu1.address       = @"中关村";
    stu1.totalScore    = @(100);
    
    // 学生2
    StudentModel *stu2 = [[StudentModel alloc] init];
    stu2.name          = @"小红";
    stu2.age           = @(19);
    stu2.address       = @"中关村";
    stu2.totalScore    = @(100);

例如上面这个,只有名字不同,所以实现copy方法。

#import <Foundation/Foundation.h>

@protocol ProtoypeCopyProtocol <NSObject>

@required

/**
 *  复制自己
 *
 *  @return 返回一个拷贝样本
 */
- (id)clone;

@end
#import <Foundation/Foundation.h>
#import "ProtoypeCopyProtocol.h"

@interface StudentModel : NSObject <ProtoypeCopyProtocol>

@property (nonatomic, strong) NSString  *name;
@property (nonatomic, strong) NSNumber  *age;
@property (nonatomic, strong) NSString  *address;
@property (nonatomic, strong) NSNumber  *totalScore;

- (id)clone;

@end
#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

重写了clone方法,将复杂的操作写在里面。

关于深拷贝和浅拷贝:

创建BaseCopyObject

#import <Foundation/Foundation.h>

@interface BasCopyObject : NSObject <NSCopying>

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

/**
 *  == 由子类重载实现 ==
 *
 *  复制(赋值操作)
 *
 *  @param object 已经复制的对象
 */
- (void)copyOperationWithObject:(id)object;

@end
#import "BasCopyObject.h"

@implementation BasCopyObject

- (id)copyWithZone:(NSZone *)zone {

    BasCopyObject *copyObject = [[self class] allocWithZone:zone];
    
    // 赋值操作作业
    [self copyOperationWithObject:copyObject];
    
    return copyObject;
}

- (void)copyOperationWithObject:(id)object {

}

@end

创建StudentModel,重写copyOperationWithObject方法,继承于StudentModel

#import "BasCopyObject.h"

@interface StudentModel : BasCopyObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *age;

@end
#import "StudentModel.h"

@implementation StudentModel

- (void)copyOperationWithObject:(StudentModel *)object {
    
    object.name = self.name;
    object.age  = self.age;
}

@end

在viewDidLoad里面实现:

  StudentModel *stu1 = [[StudentModel alloc] init];
    stu1.name          = @"小明";
    
    StudentModel *stu2 = stu1.copy;

实现浅拷贝,实现原型模式,更方便。可以打印地址,地址不同,已经完全复制了。

在创建ClassModel:

#import "BasCopyObject.h"

@interface ClassModel : BasCopyObject

@property (nonatomic, strong) NSString  *className;
@property (nonatomic, strong) NSArray   *students;

@end
#import "ClassModel.h"

@implementation ClassModel

- (void)copyOperationWithObject:(ClassModel *)object {

    object.className = self.className;
    
    // 完成了深拷贝(完整的复制了集合里面的对象)
    object.students  = [[NSArray alloc] initWithArray:self.students copyItems:YES];
}

@end

必须完成上面的这种深拷贝操作才能完成students里面stu的完全拷贝,如果用简单的object.students = self.students;只能完成浅拷贝,当一班的人改变了二班的人也会改变。

以上就是原型模式,适合在一个model里面出现了许多的属性,太少的话,就没有必要了。

原文地址:https://www.cnblogs.com/beijingxiaoguo/p/4792024.html