抽象工廠模式 objective-c 版


抽象產品A   Product.h :
ide


//
//  ProductA.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol ProductA <NSObject>
- (void)fun;
@end


具體產品A1  ProductA1.h  &  ProductA1.m :測試


//
//  ProductA1.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProductA.h"
@interface ProductA1 : NSObject <ProductA>
@end


//
//  ProductA1.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ProductA1.h"
@implementation ProductA1
- (void)fun
{
    NSLog(@"ProductA1 -> fun()");
}
@end


具體產品A2  ProductA2.h  &  ProductA2.m :get


//
//  ProductA2.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProductA.h"
@interface ProductA2 : NSObject <ProductA>
@end


//
//  ProductA2.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ProductA2.h"
@implementation ProductA2
- (void)fun
{
    NSLog(@"ProductA2 -> fun()");
}
@end


抽象產品B  ProductB.h :產品


//
//  ProductB.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol ProductB <NSObject>
- (void)fun;
@end


具體產品B1  ProductB1.h  &  ProductB1.m :it


//
//  ProductB1.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProductB.h"
@interface ProductB1 : NSObject <ProductB>
@end


//
//  ProductB1.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ProductB1.h"
@implementation ProductB1
- (void)fun
{
    NSLog(@"ProductB1 -> fun()");
}
@end


具體產品B2  ProductB2.h  &  ProductB2.m :io


//
//  ProductB2.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProductB.h"
@interface ProductB2 : NSObject <ProductB>
@end


//
//  ProductB2.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ProductB2.h"
@implementation ProductB2
- (void)fun
{
    NSLog(@"ProductB2 -> fun()");
}
@end


抽象工廠 Factory.h :class


//
//  Factory.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProductA.h"
#import "ProductB.h"
@protocol Factory <NSObject>
- (id<ProductA>) getProductA;
- (id<ProductB>) getProductB;
@end


具體工廠1  Factory1.h & Factory1.m :import


//
//  Factory1.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Factory.h"
@interface Factory1 : NSObject <Factory>
@end


//
//  Factory1.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "Factory1.h"
#import "ProductA1.h"
#import "ProductB1.h"
@implementation Factory1
- (id<ProductA>) getProductA
{
    return [[ProductA1 alloc] init];
}
- (id<ProductB>) getProductB
{
    return [[ProductB1 alloc] init];
}
@end


具體工廠2  Factory2.h &  Factory2.m :gc


//
//  Factory2.h
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Factory.h"
@interface Factory2 : NSObject <Factory>
@end


//
//  Factory2.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "Factory2.h"
#import "ProductA2.h"
#import "ProductB2.h"
@implementation Factory2
- (id<ProductA>) getProductA
{
    return [[ProductA2 alloc] init];
}
- (id<ProductB>) getProductB
{
    return [[ProductB2 alloc] init];
}
@end


測試 main.m :im


//
//  main.m
//  FactoryObc
//
//  Created by hejinlai on 13-8-8.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Factory.h"
#import "Factory1.h"
#import "Factory2.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
              
        id<Factory> factory = [[Factory1 alloc] init];
        id<ProductA> prodcutA = [factory getProductA];
        id<ProductB> prodcutB = [factory getProductB];
        [prodcutA fun];
        [prodcutB fun];
               
        id<Factory> factory2 = [[Factory2 alloc] init];
        id<ProductA> prodcutA2 = [factory2 getProductA];
        id<ProductB> prodcutB2 = [factory2 getProductB];
        [prodcutA2 fun];
        [prodcutB2 fun];
               
    }
    return 0;
}


測試結果:


2013-08-08 15:35:19.728 FactoryObc[13237:303] ProductA1 -> fun()

2013-08-08 15:35:19.730 FactoryObc[13237:303] ProductB1 -> fun()

2013-08-08 15:35:19.731 FactoryObc[13237:303] ProductA2 -> fun()

2013-08-08 15:35:19.731 FactoryObc[13237:303] ProductB2 -> fun()

相關文章
相關標籤/搜索