iOS應(yīng)用設(shè)計(jì)模式開(kāi)發(fā)中對(duì)簡(jiǎn)單工廠和工廠方法模式的運(yùn)用
簡(jiǎn)單工廠模式
正如此模式的名稱一樣,簡(jiǎn)單工廠模式基本上是所有設(shè)計(jì)模式里最簡(jiǎn)單的一種,類與類之間的關(guān)系一目了然。這次我就用很多地方經(jīng)常舉的例子--計(jì)算器,來(lái)說(shuō)明這個(gè)模式。首先給大家展示一下類之間的結(jié)構(gòu)圖:
通過(guò)這張結(jié)構(gòu)圖,可以清晰的看到,加法類、減法類、乘法類、除法類繼承自運(yùn)算類,簡(jiǎn)單工廠類依賴于運(yùn)算類的實(shí)例化來(lái)實(shí)現(xiàn)相應(yīng)的運(yùn)算功能,好的,看起來(lái)并不復(fù)雜,讓我們直接展示一下代碼吧(鑒于目前點(diǎn)點(diǎn)不支持Objective C的代碼高亮,所以就直接寫啦,盡量保持整齊吧。另,為了照顧像我一樣基礎(chǔ)不是很好的同學(xué),我盡量把代碼寫全,方便大家調(diào)試)。
注意:本文所有代碼均在ARC環(huán)境下編譯通過(guò)。
首先是運(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,無(wú)其他意義
}
@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;
}
好啦,上面羅列的是簡(jiǎn)單工廠模式的基礎(chǔ)代碼。其實(shí)還是挺簡(jiǎn)單的,對(duì)吧,只有一層繼承關(guān)系,一個(gè)依賴關(guān)系,在工廠類里面用switch語(yǔ)句判別需要實(shí)例化哪種類型,之后進(jìn)行計(jì)算,獲取結(jié)果。
工廠方法模式
上面關(guān)于簡(jiǎn)單工廠模式中就有提到過(guò)一次關(guān)于“工廠類”模式。為了幫助大家能夠回憶一下簡(jiǎn)單工廠模式,在這里提一下簡(jiǎn)單工廠模式的優(yōu)點(diǎn),簡(jiǎn)單工廠模式的最大優(yōu)點(diǎn)在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動(dòng)態(tài)實(shí)例化相關(guān)的類,對(duì)于客戶端來(lái)說(shuō),去除了與具體產(chǎn)品的依賴。其實(shí),工廠方法模式是簡(jiǎn)單工廠模式的進(jìn)一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡(jiǎn)單工廠模式的優(yōu)點(diǎn),而且克服了它的缺點(diǎn)。但缺點(diǎn)是,由于每加一個(gè)產(chǎn)品,就需要加一個(gè)產(chǎn)品工廠的類,增加了額外的開(kāi)發(fā)量。
下面還是以計(jì)算器為例子,詳細(xì)介紹工廠方法模式,還是老樣子,先向大家展示一下類結(jié)構(gòu)圖。
上面這張圖向大家展示了各個(gè)類之間的關(guān)系。其實(shí)和簡(jiǎn)單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡(jiǎn)單工廠模式多出來(lái)的抽象接口。
下面直接上代碼吧,別的不多說(shuō)了。
注意:本文所有代碼均在ARC環(huán)境下編譯通過(guò)。
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的類代碼。
- 使用設(shè)計(jì)模式中的Singleton單例模式來(lái)開(kāi)發(fā)iOS應(yīng)用程序
- 實(shí)例講解如何在iOS應(yīng)用開(kāi)發(fā)中使用設(shè)計(jì)模式中的代理模式
- IOS設(shè)計(jì)模式之組合設(shè)計(jì)模式
- IOS觀察者設(shè)計(jì)模式
- 詳解iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中Mediator中介者模式的使用
- 深入解析iOS應(yīng)用開(kāi)發(fā)中對(duì)設(shè)計(jì)模式中的橋接模式的使用
- iOS應(yīng)用運(yùn)用設(shè)計(jì)模式中的Strategy策略模式的開(kāi)發(fā)實(shí)例
- 實(shí)例講解設(shè)計(jì)模式中的命令模式在iOS App開(kāi)發(fā)中的運(yùn)用
- iOS應(yīng)用開(kāi)發(fā)中運(yùn)用設(shè)計(jì)模式中的組合模式的實(shí)例解析
- iOS App開(kāi)發(fā)中使用設(shè)計(jì)模式中的單例模式的實(shí)例解析
相關(guān)文章
iOS使用UIScorllView實(shí)現(xiàn)兩指縮放功能
兩指縮放功能不僅可以用UIPinchGestureRecognizer手勢(shì)來(lái)實(shí)現(xiàn),還能用UIScorllView來(lái)實(shí)現(xiàn),UIScrollView可以輕松的實(shí)現(xiàn)最大與最小縮放值,以及滾動(dòng)的效果,效果非常棒,具體實(shí)例代碼大家參考下本文吧2017-03-03iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫示例代碼
這篇文章主要給大家介紹了關(guān)于iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01判斷iOS應(yīng)用是否開(kāi)放HTTP權(quán)限的方法
這篇文章主要為大家詳細(xì)介紹了判斷iOS應(yīng)用是否開(kāi)放HTTP權(quán)限的方法,感興趣的小伙伴們可以參考一下2016-03-03實(shí)例講解iOS應(yīng)用開(kāi)發(fā)中UIPickerView滾動(dòng)選擇欄的用法
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中UIPickerView滾動(dòng)選擇欄的用法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS應(yīng)用開(kāi)發(fā)中使UITextField實(shí)現(xiàn)placeholder屬性的方法
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中使UITextField實(shí)現(xiàn)placeholder屬性的方法,示例代碼為傳統(tǒng)的Objective-C語(yǔ)言,需要的朋友可以參考下2016-04-04CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12iOS開(kāi)發(fā)之如何給View添加指定位置的邊框線詳解
這篇文章主要給大家介紹了iOS開(kāi)發(fā)之如何給View添加指定位置的邊框線的相關(guān)資料,給view加邊框很容易,重點(diǎn)是如何給指定邊框加邊框,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10