天天看點

Objective C--工廠方法模式

  今天和大家分享的設計模式是工廠方法模式。

  首先給出工廠方法模式的一個簡單的定義:

工廠方法模式(Factory Method),定義一個用于建立對象的接口,讓子類決定執行個體化哪一個類。工廠方法使一個類的執行個體化延遲到其子類。

  早在第一次和大家分享的關于簡單工廠模式中就有提到過一次關于“工廠類”模式。為了幫助大家能夠回憶一下簡單工廠模式,在這裡提一下簡單工廠模式的優點,簡單工廠模式的最大優點在于工廠類中包含了必要的邏輯判斷,根據用戶端的選擇條件動态執行個體化相關的類,對于用戶端來說,去除了與具體産品的依賴。其實,工廠方法模式是簡單工廠模式的進一步抽象和推廣。由于使用了多态性,工廠方法模式保持了簡單工廠模式的優點,而且克服了它的缺點。但缺點是,由于每加一個産品,就需要加一個産品工廠的類,增加了額外的開發量。

  下面還是以電腦為例子,詳細介紹工廠方法模式,還是老樣子,先向大家展示一下類結構圖。

Objective C--工廠方法模式

  上面這張圖向大家展示了各個類之間的關系。其實和簡單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡單工廠模式多出來的抽象接口。

  下面直接上代碼吧,别的不多說了。

  • Operation類接口
1 2 3 4 5 6 7 8 9 10

#import <Foundation/Foundation.h>

@interface

Operation:

NSObject

{

double

numberA;

double

numberB;

}

@property

double

numberA;

@property

double

numberB;

-(

double

) GetResult;

@end

  • Operation類實作
1 2 3 4 5 6 7 8

#import "Operation.h"

@implementation

Operation

@synthesize

numberA, numberB;

-(

double

) GetResult{

return

-1.0;

}

@end

  • OperationAdd類接口
1 2 3 4

#import "Operation.h"

@interface

OperationAdd :Operation

@end

  • OperationAdd類實作
1 2 3 4 5 6 7 8 9

#import "OperationAdd.h"

@implementation

OperationAdd

-(

double

) GetResult{

double

result =0;

result = numberA+numberB;

return

result;

}

@end

  • OperationDiv類接口
1 2 3 4

#import "Operation.h"

@interface

OperationDiv:Operation

@end

  • OperationDiv類實作
1 2 3 4 5 6 7 8 9 10 11 12 13 14

#import "OperationDiv.h"

@implementation

OperationDiv

-(

double

)GetResult{

double

result =0;

@try

{

result = numberA/numberB;

}

@catch

(

NSException

*exception) {

NSLog

(@

"除數不能為0"

);

}

return

result;

}

@end

  • OperationMul類接口
1 2 3 4

#import "Operation.h"

@interface

OperationMul:Operation

@end

  • OperationMul類實作
1 2 3 4 5 6 7 8 9

#import "OperationMul.h"

@implementation

OperationMul

-(

double

)GetResult{

double

result =0;

result = numberA*numberB;

return

result;

}

@end

  • OperationSub類接口
1 2 3 4

#import "Operation.h"

@interface

OperationSub :Operation

@end

  • OperationSub類接口
1 2 3 4 5 6 7 8 9

#import "OperationSub.h"

@implementation

OperationSub

-(

double

)GetResult{

double

result =0;

result = numberA-numberB;

return

result;

}

@end

  • IFactory類接口
1 2 3 4 5 6

#import <Foundation/Foundation.h>

#import "Operation.h"

@interface

IFactory :

NSObject

-(Operation*)CreateOperation;

@end

  • IFactory類實作
1 2 3 4 5 6 7

#import "IFactory.h"

@implementation

IFactory

-(Operation*)CreateOperation{

return

[[Operation alloc]init];

}

@end

  • AddFactory類接口
1 2 3 4

#import "IFactory.h"

@interface

AddFactory:IFactory

@end

  • AddFactory類實作
1 2 3 4 5 6 7 8

#import "AddFactory.h"

#import "OperationAdd.h"

@implementation

AddFactory

-(Operation*)CreateOperation{

return

[[OperationAdd alloc]init];

}

@end

  • SubFactory類接口
1 2 3 4

#import "IFactory.h"

@interface

SubFactory:IFactory

@end

  • SubFactory類實作
1 2 3 4 5 6 7 8

#import "SubFactory.h"

#import "OperationSub.h"

@implementation

SubFactory

-(Operation*)CreateOperation{

return

[[OperationSub alloc]init];

}

@end

  • MulFactory類接口
1 2 3 4

#import "IFactory.h"

@interface

MulFactory:IFactory

@end

  • MulFactory類實作
1 2 3 4 5 6 7 8

#import "MulFactory.h"

#import "OperationMul.h"

@implementation

MulFactory

-(Operation*)CreateOperation{

return

[[OperationMul alloc]init];

}

@end

  • DivFactory類接口
1 2 3 4

#import "IFactory.h"

@interfaceDiv

Factory:IFactory

@end

  • DivFactory類實作
1 2 3 4 5 6 7 8

#import "DivFactory.h"

#import "OperationDiv.h"

@implementation

DivFactory

-(Operation*)CreateOperation{

return

[[OperationDiv alloc]init];

}

@end

  • Main方法調用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

#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的類代碼。

繼續閱讀