设计模式,很早接触到软件编程的时候,就经常听到人说,设计模式的灵活应用是高级软件工程师必备,以及各种高大上的修饰.最初接触设计模式,应该是借同学的<大话设计模式>,在这里推荐一下,蛮不错的.然后,最火的应该是GOF的23种设计模式,不过我没怎么看,^_^.随着自身学习和工作的不断加深,觉得很有必要认真仔细的去研究一下了,因为自身主要开发iOS,所以,参考我标题的这本书为主.
@ 基本描述
#import <Foundation/Foundation.h> @interface HMTClothes : NSObject // 展示衣服类型 - (void)showClothesType; @end #import "HMTClothes.h" @implementation HMTClothes - (void)showClothesType{ } @end #import "HMTClothes.h" @interface HMTClothes_Dota : HMTClothes - (void)showClothesType; @end #import "HMTClothes_Dota.h" @implementation HMTClothes_Dota - (void)showClothesType{ NSLog(@"这件衣服的类型是Dota"); } @end #import "HMTClothes.h" @interface HMTClothes_LOL : HMTClothes - (void)showClothesType; @end #import "HMTClothes_LOL.h" @implementation HMTClothes_LOL - (void)showClothesType{ NSLog(@"这件衣服的类型是LOL"); } @end #import <Foundation/Foundation.h> @class HMTClothes; @interface HMTClothesFactory : NSObject - (HMTClothes *)makeClothes; @end #import "HMTClothesFactory.h" @implementation HMTClothesFactory - (HMTClothes *)makeClothes{ return nil; } @end #import "HMTClothesFactory.h" @interface HMTClothesDotaFactory : HMTClothesFactory - (HMTClothes *)makeClothes; @end #import "HMTClothesDotaFactory.h" #import "HMTClothes_Dota.h" @implementation HMTClothesDotaFactory - (HMTClothes *)makeClothes{ return [[HMTClothes_Dota alloc] init]; } @end #import "HMTClothesFactory.h" @interface HMTClothesLOLFactory : HMTClothesFactory - (HMTClothes *)makeClothes; @end #import "HMTClothesLOLFactory.h" #import "HMTClothes_LOL.h" @implementation HMTClothesLOLFactory - (HMTClothes *)makeClothes{ return [[HMTClothes_LOL alloc] init]; } @end - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. /** * 实例变量的类型,是由进行了alloc的Class决定的,一般也就是等号的右边 * 就好比,NSMutableArray * mutableArray = 一个类型为NSArray的数组;它的实质还是一个NSArray类型,并 * 不能调用NSMutableArray的方法 */ HMTClothesFactory *clothesFactory = [[HMTClothesDotaFactory alloc] init]; HMTClothes *clothes = [clothesFactory makeClothes]; [clothes showClothesType]; // -->输出的是"这件衣服的类型是Dota" // 如果你想制造LOL衣服,直接把HMTClothesDotaFactory->HMTClothesLOLFactory即可 }