簡單工廠模式
正如此模式的名稱一樣,簡單工廠模式基本上是所有設計模式裡最簡單的一種,類與類之間的關系一目了然。這次我就用很多地方經常舉的例子--計算器,來說明這個模式。首先給大家展示一下類之間的結構圖:
通過這張結構圖,可以清晰的看到,加法類、減法類、乘法類、除法類繼承自運算類,簡單工廠類依賴於運算類的實例化來實現相應的運算功能,好的,看起來並不復雜,讓我們直接展示一下代碼吧(鑒於目前點點不支持Objective C的代碼高亮,所以就直接寫啦,盡量保持整齊吧。另,為了照顧像我一樣基礎不是很好的同學,我盡量把代碼寫全,方便大家調試)。
注意:本文所有代碼均在ARC環境下編譯通過。
首先是運算類(父類):
接口文件:
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
實現文件:
復制代碼 代碼如下:
#import"Operation.h"
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0; //此處默認返回-1.0,無其他意義
}
@end
加法類(運算子類):
接口文件:
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationAdd:Operation
@end
實現文件:
復制代碼 代碼如下:
#import "OperationAdd.h"
@implementation OperationAdd
-(double) GetResult{
double result =0;
result =numberA+numberB;
return result;
}
@end
減法類(運算子類):
接口文件:
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationSub:Operation
@end
實現文件:
復制代碼 代碼如下:
#import "OperationSub.h"
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
乘法類(運算子類)
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationMul:Operation
@end
實現文件:
復制代碼 代碼如下:
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
除法類(運算子類):
接口文件:
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationDiv:Operation
@end
實現文件:
復制代碼 代碼如下:
#import "OperationDiv.h"
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除數不能為0");
}
return result;
}
@end
下面是工廠類(依賴實力化運算類實現具體功能):
接口文件:
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationSub.h"
#import "OperationMul.h"
@interface OperationFactory:NSObject
+(Operation*)CreateOperate:(char)operate;
@end
實現文件:
復制代碼 代碼如下:
#import "OperationFactory.h"
+(Operation*)CreateOperate:(char)operate{
Operation *oper;
switch(operate) {
case '+':
oper = [[OperationAdd alloc]init];
break;
case '-':
oper = [[OperationSub alloc]init];
break;
case '*':
oper = [[OperationMul alloc]init];
break;
case '/':
oper = [[OperationDiv alloc]init];
break;
default:
oper = nil;
break;
}
return oper;
}
具體調用
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationMul.h"
#import "OperationSub.h"
#import "OperationFactory.h"
int main (int argc,const char* argv[])
{
@autoreleasepool{
Operation *oper = [OperationFactory CreateOperate:'*'];
[oper setNumberA:1];
[oper setNumberB:2];
double result = 0;
result = [oper GetResult];
NSLog(@"Result is %f", result);
}
return 0;
}
好啦,上面羅列的是簡單工廠模式的基礎代碼。其實還是挺簡單的,對吧,只有一層繼承關系,一個依賴關系,在工廠類裡面用switch語句判別需要實例化哪種類型,之後進行計算,獲取結果。
工廠方法模式
上面關於簡單工廠模式中就有提到過一次關於“工廠類”模式。為了幫助大家能夠回憶一下簡單工廠模式,在這裡提一下簡單工廠模式的優點,簡單工廠模式的最大優點在於工廠類中包含了必要的邏輯判斷,根據客戶端的選擇條件動態實例化相關的類,對於客戶端來說,去除了與具體產品的依賴。其實,工廠方法模式是簡單工廠模式的進一步抽象和推廣。由於使用了多態性,工廠方法模式保持了簡單工廠模式的優點,而且克服了它的缺點。但缺點是,由於每加一個產品,就需要加一個產品工廠的類,增加了額外的開發量。
下面還是以計算器為例子,詳細介紹工廠方法模式,還是老樣子,先向大家展示一下類結構圖。
上面這張圖向大家展示了各個類之間的關系。其實和簡單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡單工廠模式多出來的抽象接口。
下面直接上代碼吧,別的不多說了。
注意:本文所有代碼均在ARC環境下編譯通過。
Operation類接口
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
Operation類實現
復制代碼 代碼如下:
#import "Operation.h"
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0;
}
@end
OperationAdd類接口
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationAdd :Operation
@end
OperationAdd類實現
復制代碼 代碼如下:
#import "OperationAdd.h"
@implementation OperationAdd
-(double) GetResult{
double result =0;
result = numberA+numberB;
return result;
}
@end
OperationDiv類接口
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationDiv :Operation
@end
OperationDiv類實現
復制代碼 代碼如下:
#import "OperationDiv.h"
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除數不能為0");
}
return result;
}
@end
OperationMul類接口
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationMul :Operation
@end
OperationMul類實現
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
OperationSub類接口
復制代碼 代碼如下:
#import "Operation.h"
@interface OperationSub :Operation
@end
OperationSub類實現
復制代碼 代碼如下:
#import "OperationSub.h"
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
IFactory類接口
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
#import "Operation.h"
@interface IFactory :NSObject
-(Operation*)CreateOperation;
@end
IFactory類實現
復制代碼 代碼如下:
#import "IFactory.h"
@implementation IFactory
-(Operation*)CreateOperation{
return [[Operation alloc]init];
}
@end
AddFactory類接口
復制代碼 代碼如下:
#import "IFactory.h"
@interface AddFactory :IFactory
@end
AddFactory類實現
復制代碼 代碼如下:
#import "AddFactory.h"
#import "OperationAdd.h"
@implementation AddFactory
-(Operation*)CreateOperation{
return [[OperationAdd alloc]init];
}
@end
SubFactory類接口
復制代碼 代碼如下:
#import "IFactory.h"
@interface SubFactory :IFactory
@end
SubFactory類實現
復制代碼 代碼如下:
#import "SubFactory.h"
#import "OperationSub.h"
@implementation SubFactory
-(Operation*)CreateOperation{
return [[OperationSub alloc]init];
}
@end
MulFactory類接口
復制代碼 代碼如下:
#import "IFactory.h"
@interface MulFactory :IFactory
@end
MulFactory類實現
復制代碼 代碼如下:
#import "MulFactory.h"
#import "OperationMul.h"
@implementation MulFactory
-(Operation*)CreateOperation{
return [[OperationMul alloc]init];
}
@end
DivFactory類接口
復制代碼 代碼如下:
#import "IFactory.h"
@interfaceDiv Factory :IFactory
@end
DivFactory類實現
復制代碼 代碼如下:
#import "DivFactory.h"
#import "OperationDiv.h"
@implementation DivFactory
-(Operation*)CreateOperation{
return [[OperationDiv alloc]init];
}
@end
Main方法調用
復制代碼 代碼如下:
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h" //加法工廠,你可以根據需要添加其他運算工廠
int main (int argc,const char* argv[])
{
@autoreleasepool{
IFactory *operFactory = [[AddFactory alloc]init];
Operation *oper = [operFactory CreateOperation];
[oper setNumberA:1];
[oper setNumberB:2];
double result = [oper GetResult];
NSLog(@"The result is %f", result);
}
return 0;
}
好啦,上面就是工廠方法模式的Objective C的類代碼。