抽象原型 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] zhangsan

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