设计模式-原型模式

用原型实例指定创建对象的种类,并且通过拷贝这些对象原型创建新的对象

一、使用场景:

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值