原型模式 objective-c 版


抽象原型 Prototype.h  &  Prototype.mide


//
//  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.matom


//
//  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] zhangsanit

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

相關文章
相關標籤/搜索