日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

抽象工厂模式 objective-c 版

發布時間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 抽象工厂模式 objective-c 版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


抽象產品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


具體產品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 :


// // 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 :


// // 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 :


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


測試 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; }


測試結果:


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()


轉載于:https://blog.51cto.com/ikinglai/1266998

總結

以上是生活随笔為你收集整理的抽象工厂模式 objective-c 版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。