您的位置:首页 > 移动开发 > Objective-C

原型模式 objective-c 版

2013-08-09 10:51 183 查看
抽象原型 Prototype.h & Prototype.m

//
//  Prototype.h
//  PrototypeObc
//
//  Created by hejinlai on 13-8-9.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Prototype : NSObject <NSCopying>
@property (nonatomic, assign) NSString * name;
@end


//
//  Prototype.m
//  PrototypeObc
//
//  Created by hejinlai on 13-8-9.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "Prototype.h"
@implementation Prototype
@synthesize name = _name;
-(id)copyWithZone:(NSZone *)zone
{
Prototype * p = [[self class] allocWithZone:zone];
p.name = _name;
return p;
}
@end


具体原型 ConcretePrototype.h & ConcretePrototype.m :

//
//  ConcretePrototype.h
//  PrototypeObc
//
//  Created by hejinlai on 13-8-9.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Prototype.h"
@interface ConcretePrototype : Prototype
@end


//
//  ConcretePrototype.m
//  PrototypeObc
//
//  Created by hejinlai on 13-8-9.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ConcretePrototype.h"
@implementation ConcretePrototype
@end


测试 main.m

//
//  main.m
//  PrototypeObc
//
//  Created by hejinlai on 13-8-9.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ConcretePrototype.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {

Prototype * p = [[ConcretePrototype alloc] init];
p.name = @"zhangsan";
NSLog(@"%@", p.name);

Prototype * p2 = p.copy;
NSLog(@"%@", p2.name);

}
return 0;
}


测试结果:

2013-08-09 10:44:40.020 PrototypeObc[1049:303] zhangsan2013-08-09 10:44:40.022 PrototypeObc[1049:303] zhangsan

本文出自 “移动开发” 博客,请务必保留此出处http://ikinglai.blog.51cto.com/6220785/1268421
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: