簡單工廠模式
正如此模式的名稱一樣,簡單工廠模式基本上是所有設(shè)計模式里最簡單的一種,類與類之間的關(guān)系一目了然。這次我就用很多地方經(jīng)常舉的例子--計算器,來說明這個模式。首先給大家展示一下類之間的結(jié)構(gòu)圖:
通過這張結(jié)構(gòu)圖,可以清晰的看到,加法類、減法類、乘法類、除法類繼承自運(yùn)算類,簡單工廠類依賴于運(yùn)算類的實(shí)例化來實(shí)現(xiàn)相應(yīng)的運(yùn)算功能,好的,看起來并不復(fù)雜,讓我們直接展示一下代碼吧(鑒于目前點(diǎn)點(diǎn)不支持objective c的代碼高亮,所以就直接寫啦,盡量保持整齊吧。另,為了照顧像我一樣基礎(chǔ)不是很好的同學(xué),我盡量把代碼寫全,方便大家調(diào)試)。
注意:本文所有代碼均在arc環(huán)境下編譯通過。
首先是運(yùn)算類(父類):
接口文件:
#import <foundation/foundation.h>
@interface operation :nsobject{
double numbera;
double numberb;
}
@property double numbera;
@property double numberb;
-(double) getresult;
@end
實(shí)現(xiàn)文件:
#import"operation.h"
@implementation operation
@synthesize numbera, numberb;
-(double) getresult{
return -1.0; //此處默認(rèn)返回-1.0,無其他意義
}
@end
加法類(運(yùn)算子類):
接口文件:
#import "operation.h"
@interface operationadd:operation
@end
實(shí)現(xiàn)文件:
#import "operationadd.h"
@implementation operationadd
-(double) getresult{
double result =0;
result =numbera+numberb;
return result;
}
@end
減法類(運(yùn)算子類):
接口文件:
#import "operation.h"
@interface operationsub:operation
@end
實(shí)現(xiàn)文件:
#import "operationsub.h"
@implementation operationsub
-(double)getresult{
double result =0;
result = numbera-numberb;
return result;
}
@end
乘法類(運(yùn)算子類)
#import "operation.h"
@interface operationmul:operation
@end
實(shí)現(xiàn)文件:
#import "operationmul.h"
@implementation operationmul
-(double)getresult{
double result =0;
result = numbera*numberb;
return result;
}
@end
除法類(運(yùn)算子類):
接口文件:
#import "operation.h"
@interface operationdiv:operation
@end
實(shí)現(xiàn)文件:
#import "operationdiv.h"
@implementation operationdiv
-(double)getresult{
double result =0;
@try{
result = numbera/numberb;
}
@catch(nsexception *exception) {
nslog(@"除數(shù)不能為0");
}
return result;
}
@end
下面是工廠類(依賴實(shí)力化運(yùn)算類實(shí)現(xiàn)具體功能):
接口文件:
#import <foundation/foundation.h>
#import "operationadd.h"
#import "operationdiv.h"
#import "operationsub.h"
#import "operationmul.h"
@interface operationfactory:nsobject
+(operation*)createoperate:(char)operate;
@end
實(shí)現(xiàn)文件:
#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;
}
具體調(diào)用
#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;
}
好啦,上面羅列的是簡單工廠模式的基礎(chǔ)代碼。其實(shí)還是挺簡單的,對吧,只有一層繼承關(guān)系,一個依賴關(guān)系,在工廠類里面用switch語句判別需要實(shí)例化哪種類型,之后進(jìn)行計算,獲取結(jié)果。
工廠方法模式
上面關(guān)于簡單工廠模式中就有提到過一次關(guān)于“工廠類”模式。為了幫助大家能夠回憶一下簡單工廠模式,在這里提一下簡單工廠模式的優(yōu)點(diǎn),簡單工廠模式的最大優(yōu)點(diǎn)在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實(shí)例化相關(guān)的類,對于客戶端來說,去除了與具體產(chǎn)品的依賴。其實(shí),工廠方法模式是簡單工廠模式的進(jìn)一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡單工廠模式的優(yōu)點(diǎn),而且克服了它的缺點(diǎn)。但缺點(diǎn)是,由于每加一個產(chǎn)品,就需要加一個產(chǎn)品工廠的類,增加了額外的開發(fā)量。
下面還是以計算器為例子,詳細(xì)介紹工廠方法模式,還是老樣子,先向大家展示一下類結(jié)構(gòu)圖。
上面這張圖向大家展示了各個類之間的關(guān)系。其實(shí)和簡單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡單工廠模式多出來的抽象接口。
下面直接上代碼吧,別的不多說了。
注意:本文所有代碼均在arc環(huán)境下編譯通過。
operation類接口
#import <foundation/foundation.h>
@interface operation :nsobject{
double numbera;
double numberb;
}
@property double numbera;
@property double numberb;
-(double) getresult;
@end
operation類實(shí)現(xiàn)
#import "operation.h"
@implementation operation
@synthesize numbera, numberb;
-(double) getresult{
return -1.0;
}
@end
operationadd類接口
#import "operation.h"
@interface operationadd :operation
@end
operationadd類實(shí)現(xiàn)
#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類實(shí)現(xiàn)
#import "operationdiv.h"
@implementation operationdiv
-(double)getresult{
double result =0;
@try{
result = numbera/numberb;
}
@catch(nsexception *exception) {
nslog(@"除數(shù)不能為0");
}
return result;
}
@end
operationmul類接口
#import "operation.h"
@interface operationmul :operation
@end
operationmul類實(shí)現(xiàn)
#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類實(shí)現(xiàn)
#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類實(shí)現(xiàn)
#import "ifactory.h"
@implementation ifactory
-(operation*)createoperation{
return [[operation alloc]init];
}
@end
addfactory類接口
#import "ifactory.h"
@interface addfactory :ifactory
@end
addfactory類實(shí)現(xiàn)
#import "addfactory.h"
#import "operationadd.h"
@implementation addfactory
-(operation*)createoperation{
return [[operationadd alloc]init];
}
@end
subfactory類接口
#import "ifactory.h"
@interface subfactory :ifactory
@end
subfactory類實(shí)現(xiàn)
#import "subfactory.h"
#import "operationsub.h"
@implementation subfactory
-(operation*)createoperation{
return [[operationsub alloc]init];
}
@end
mulfactory類接口
#import "ifactory.h"
@interface mulfactory :ifactory
@end
mulfactory類實(shí)現(xiàn)
#import "mulfactory.h"
#import "operationmul.h"
@implementation mulfactory
-(operation*)createoperation{
return [[operationmul alloc]init];
}
@end
divfactory類接口
#import "ifactory.h"
@interfacediv factory :ifactory
@end
divfactory類實(shí)現(xiàn)
#import "divfactory.h"
#import "operationdiv.h"
@implementation divfactory
-(operation*)createoperation{
return [[operationdiv alloc]init];
}
@end
main方法調(diào)用
#import <foundation/foundation.h>
#import "operationadd.h"
#import "addfactory.h" //加法工廠,你可以根據(jù)需要添加其他運(yùn)算工廠
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的類代碼。