用原型实例指定创建对象的种类,并且通过拷贝这些对象原型创建新的对象
一、使用场景:
1. 当一个类的实例之间存在差异,而这些差异仅是状态的若干组合,复制原型要比手工实例化更加方便
2. 当一个类并不容易创建(比如有着复杂的组合对象),且复制已有的组合对象要比对副本修改更加容易,此时,可以考虑使用原型模式
二、优点
内存二进制流的拷贝,比直接new一个对象性能好,特别是要在一个循环体内产生大量对象
三、注意事项
深浅拷贝问题
四、源码:https://github.com/baitxaps/ProtoType
@interface AdvTemplate : NSObject<NSCopying>
@property(nonatomic,strong)NSString *advSubject;
@property(nonatomic,strong)NSString *advContext;
- (instancetype)init;
+ (instancetype)init;
@end
@implementation AdvTemplate
- (instancetype)init
{
if (self =[superinit]) {
self.advSubject =@"XX银行国庆信用卡抽奖活动";;
self.advContext =@"国庆抽奖活动通知:只要刷卡就送你一百万!...";
}
return self;
}
+ (instancetype)init
{
return [[selfalloc]init];
}
-(id)copyWithZone:(NSZone *)zone
{
AdvTemplate *copy = [[selfclass]allocWithZone:zone];
copy.advContext =self.advContext;
copy.advSubject =self.advSubject;
return copy;
}
@end
#import "AdvTemplate.h"
@interface Mail : NSObject <NSCopying>
@property(nonatomic,strong)NSString *receiver;
@property(nonatomic,strong)NSString *subject;
@property(nonatomic,strong)NSString *appellation;
@property(nonatomic,strong)NSString *contxt;
@property(nonatomic,strong)NSString *tail;
@property(nonatomic,strong)NSArray *datas;
@property(nonatomic,strong)NSDictionary *infomation;
- (instancetype)initWithObj:(AdvTemplate *)obj;
+ (instancetype)initWithObj:(AdvTemplate *)obj;
- (id)copyWithZone:(NSZone *)zone;
@end
#import "Mail.h"
@implementation Mail
- (instancetype)initWithObj:(AdvTemplate *)obj
{
if (self = [superinit]) {
self.contxt = obj.advContext;
self.subject = obj.advSubject;
self.infomation =@{@"obj":obj};
self.datas =@[obj];
}
return self;
}
+ (instancetype)initWithObj:(AdvTemplate *)obj
{
return [[selfalloc] initWithObj:obj];
}
- (id)copyWithZone:(NSZone *)zone
{
Mail *copy = [[selfclass] allocWithZone:zone];
copy.contxt = self.contxt;
copy.subject = self.subject;
copy.appellation =self.appellation;
copy.tail = self.tail;
copy.receiver = self.receiver;
copy.datas = \
[[[self.datasclass]alloc]initWithArray:self.datascopyItems:YES];
copy.infomation = \
[[[self.infomationclass]alloc]initWithDictionary:self.infomationcopyItems:YES];
return copy;
}
@end
- (void)viewDidLoad
{
[superviewDidLoad];
int maxCount= 6;
int i =0;
Mail *mail = [MailinitWithObj:[AdvTemplatenew]];
mail.tail = @"XX银行版权所有";
while (i <maxCount) {
Mail *cloneMail = mail.copy;
cloneMail.appellation = [NSStringstringWithFormat:@"%@先生(女士)",[selfrandString:5]];
cloneMail.receiver = [NSStringstringWithFormat:@"%@@%@%@",[selfrandString:5],[selfrandString:8],@".com"];
[self sendMail:cloneMail];
i++;
}
}
-(void)sendMail:(Mail *)mail
{
NSLog(@"标题:%@ \t收件人:%@ \t...发送成功!",mail.subject,mail.receiver );
NSLog(@"detail:%@,%@",mail.datas,mail.infomation);
}
-(NSString *)randString:(NSInteger)maxLength
{
NSString *string =@"abcdefghijklnmopqrestuvwxyzABCDEFGHIJKLMNOPQRESTUVWXYZ";
NSMutableString *sb = [NSMutableStringnew];
srand((unsigned)time(NULL));
for (int i =0; i< maxLength; i++) {
int rand = arc4random()%maxLength;
[sb appendString:[stringsubstringWithRange:NSMakeRange(rand,1)]];
}
return sb;
}
@end