iOS设计模式--工厂模式

来源:互联网 发布:win10 pe 网络全能版 编辑:程序博客网 时间:2024/06/06 16:26

       设计模式,很早接触到软件编程的时候,就经常听到人说,设计模式的灵活应用是高级软件工程师必备,以及各种高大上的修饰.最初接触设计模式,应该是借同学的<大话设计模式>,在这里推荐一下,蛮不错的.然后,最火的应该是GOF的23种设计模式,不过我没怎么看,^_^.随着自身学习和工作的不断加深,觉得很有必要认真仔细的去研究一下了,因为自身主要开发iOS,所以,参考我标题的这本书为主.

       

@ 基本描述

GOF是这样描述工厂模式的:
“Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”
在基类中定义创建对象的一个接口,让子类决定实例化哪个类。工厂方法让一个类的实例化延迟到子类中进行。
工厂方法要解决的问题是对象的创建时机,它提供了一种扩展的策略,很好地符合了开放封闭原则。工厂方法也叫做虚构造器(Virtual Constructor).

@什么时候使用工厂方法?


@iOS中工厂方法的简单实现
        举个例子,有一家生产衣服的工厂,它生产2种型号的衣服,一个为DOTA类,一个为LOL类,经销商需要进什么类型的货一定要显示的告诉工厂,生产指定的类型,这样很麻烦,后来它有钱了,开了两家工厂,一家单独生产DOTA类,一家单独生产LOL类,这样,经销商进货直接去找对应的工厂就行.
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface HMTClothes : NSObject  
  4.   
  5. //  展示衣服类型  
  6. - (void)showClothesType;  
  7.   
  8. @end  
  9.   
  10. #import "HMTClothes.h"  
  11.   
  12. @implementation HMTClothes  
  13. - (void)showClothesType{  
  14. }  
  15. @end  
  16.   
  17. #import "HMTClothes.h"  
  18.   
  19. @interface HMTClothes_Dota : HMTClothes  
  20.   
  21. - (void)showClothesType;  
  22.   
  23. @end  
  24.   
  25. #import "HMTClothes_Dota.h"  
  26.   
  27. @implementation HMTClothes_Dota  
  28.   
  29. - (void)showClothesType{  
  30.     NSLog(@"这件衣服的类型是Dota");  
  31. }  
  32. @end  
  33.   
  34. #import "HMTClothes.h"  
  35.   
  36. @interface HMTClothes_LOL : HMTClothes  
  37.   
  38. - (void)showClothesType;  
  39.   
  40. @end  
  41.   
  42. #import "HMTClothes_LOL.h"  
  43.   
  44. @implementation HMTClothes_LOL  
  45.   
  46. - (void)showClothesType{  
  47.     NSLog(@"这件衣服的类型是LOL");  
  48. }  
  49.   
  50. @end  
  51.   
  52. #import <Foundation/Foundation.h>  
  53.   
  54. @class HMTClothes;  
  55. @interface HMTClothesFactory : NSObject  
  56.   
  57. - (HMTClothes *)makeClothes;  
  58.   
  59. @end  
  60.   
  61. #import "HMTClothesFactory.h"  
  62.   
  63. @implementation HMTClothesFactory  
  64.   
  65. - (HMTClothes *)makeClothes{  
  66.     return nil;  
  67. }  
  68. @end  
  69.   
  70. #import "HMTClothesFactory.h"  
  71.   
  72. @interface HMTClothesDotaFactory : HMTClothesFactory  
  73.   
  74. - (HMTClothes *)makeClothes;  
  75.   
  76. @end  
  77.   
  78. #import "HMTClothesDotaFactory.h"  
  79. #import "HMTClothes_Dota.h"  
  80.   
  81. @implementation HMTClothesDotaFactory  
  82.   
  83. - (HMTClothes *)makeClothes{  
  84.   return [[HMTClothes_Dota alloc] init];  
  85. }  
  86. @end  
  87.   
  88. #import "HMTClothesFactory.h"  
  89.   
  90. @interface HMTClothesLOLFactory : HMTClothesFactory  
  91.   
  92. - (HMTClothes *)makeClothes;  
  93.   
  94. @end  
  95.   
  96. #import "HMTClothesLOLFactory.h"  
  97. #import "HMTClothes_LOL.h"  
  98. @implementation HMTClothesLOLFactory  
  99.   
  100. - (HMTClothes *)makeClothes{  
  101.     return [[HMTClothes_LOL alloc] init];  
  102. }  
  103. @end  
  104.   
  105. - (void)viewDidLoad  
  106. {  
  107.     [super viewDidLoad];  
  108.     // Do any additional setup after loading the view.  
  109.       
  110.     /** 
  111.      *  实例变量的类型,是由进行了alloc的Class决定的,一般也就是等号的右边 
  112.      *  就好比,NSMutableArray * mutableArray = 一个类型为NSArray的数组;它的实质还是一个NSArray类型,并 
  113.      *  不能调用NSMutableArray的方法 
  114.      */  
  115.     HMTClothesFactory *clothesFactory = [[HMTClothesDotaFactory alloc] init];  
  116.     HMTClothes *clothes = [clothesFactory makeClothes];  
  117.     [clothes showClothesType];  //  -->输出的是"这件衣服的类型是Dota"  
  118.     //  如果你想制造LOL衣服,直接把HMTClothesDotaFactory->HMTClothesLOLFactory即可  
  119.  }  

0 0
原创粉丝点击