iOS應用設計模式開發(fā)中對簡單工廠和工廠方法模式的運用
簡單工廠模式
正如此模式的名稱一樣,簡單工廠模式基本上是所有設計模式里最簡單的一種,類與類之間的關系一目了然。這次我就用很多地方經(jīng)常舉的例子--計算器,來說明這個模式。首先給大家展示一下類之間的結構圖:
通過這張結構圖,可以清晰的看到,加法類、減法類、乘法類、除法類繼承自運算類,簡單工廠類依賴于運算類的實例化來實現(xiàn)相應的運算功能,好的,看起來并不復雜,讓我們直接展示一下代碼吧(鑒于目前點點不支持Objective C的代碼高亮,所以就直接寫啦,盡量保持整齊吧。另,為了照顧像我一樣基礎不是很好的同學,我盡量把代碼寫全,方便大家調試)。
注意:本文所有代碼均在ARC環(huán)境下編譯通過。
首先是運算類(父類):
接口文件:
#import <Foundation/Foundation.h>
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
實現(xiàn)文件:
#import"Operation.h"
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0; //此處默認返回-1.0,無其他意義
}
@end
加法類(運算子類):
接口文件:
#import "Operation.h"
@interface OperationAdd:Operation
@end
實現(xiàn)文件:
#import "OperationAdd.h"
@implementation OperationAdd
-(double) GetResult{
double result =0;
result =numberA+numberB;
return result;
}
@end
減法類(運算子類):
接口文件:
#import "Operation.h"
@interface OperationSub:Operation
@end
實現(xiàn)文件:
#import "OperationSub.h"
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
乘法類(運算子類)
#import "Operation.h"
@interface OperationMul:Operation
@end
實現(xiàn)文件:
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
除法類(運算子類):
接口文件:
#import "Operation.h"
@interface OperationDiv:Operation
@end
實現(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
下面是工廠類(依賴實力化運算類實現(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
實現(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;
}
具體調用
#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語句判別需要實例化哪種類型,之后進行計算,獲取結果。
工廠方法模式
上面關于簡單工廠模式中就有提到過一次關于“工廠類”模式。為了幫助大家能夠回憶一下簡單工廠模式,在這里提一下簡單工廠模式的優(yōu)點,簡單工廠模式的最大優(yōu)點在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實例化相關的類,對于客戶端來說,去除了與具體產品的依賴。其實,工廠方法模式是簡單工廠模式的進一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡單工廠模式的優(yōu)點,而且克服了它的缺點。但缺點是,由于每加一個產品,就需要加一個產品工廠的類,增加了額外的開發(fā)量。
下面還是以計算器為例子,詳細介紹工廠方法模式,還是老樣子,先向大家展示一下類結構圖。
上面這張圖向大家展示了各個類之間的關系。其實和簡單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡單工廠模式多出來的抽象接口。
下面直接上代碼吧,別的不多說了。
注意:本文所有代碼均在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類實現(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類實現(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類實現(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類實現(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類實現(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類實現(xiàn)
#import "IFactory.h"
@implementation IFactory
-(Operation*)CreateOperation{
return [[Operation alloc]init];
}
@end
AddFactory類接口
#import "IFactory.h"
@interface AddFactory :IFactory
@end
AddFactory類實現(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類實現(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類實現(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類實現(xiàn)
#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" //加法工廠,你可以根據(jù)需要添加其他運算工廠
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的類代碼。
相關文章
iOS使用UIScorllView實現(xiàn)兩指縮放功能
兩指縮放功能不僅可以用UIPinchGestureRecognizer手勢來實現(xiàn),還能用UIScorllView來實現(xiàn),UIScrollView可以輕松的實現(xiàn)最大與最小縮放值,以及滾動的效果,效果非常棒,具體實例代碼大家參考下本文吧2017-03-03實例講解iOS應用開發(fā)中UIPickerView滾動選擇欄的用法
這篇文章主要介紹了iOS應用開發(fā)中UIPickerView滾動選擇欄的用法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法
這篇文章主要介紹了iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-04-04CAMediaTiming ( 時間協(xié)議)詳解及實例代碼
這篇文章主要介紹了CAMediaTiming / 時間協(xié)議詳解及實例代碼的相關資料,這里附有實例代碼,幫助大家學習參考,需要的朋友可以參考下2016-12-12