抽象產(chǎn)品A ? Product.h :
//
// 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
具體產(chǎn)品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
具體產(chǎn)品A2 ?ProductA2.h ?& ?ProductA2.m :
//
// 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
抽象產(chǎn)品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
具體產(chǎn)品B1 ?ProductB1.h ?& ?ProductB1.m :
//
// 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
具體產(chǎn)品B2 ?ProductB2.h ?& ?ProductB2.m :
//
// 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 :
//
// 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 :
//
// 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 :
//
// 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
測(cè)試 main.m :
//
// 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;
}
測(cè)試結(jié)果:
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()
轉(zhuǎn)載于:https://blog.51cto.com/ikinglai/1266998
總結(jié)
以上是生活随笔為你收集整理的抽象工厂模式 objective-c 版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。