豐富了類的實現模式, 父類可以聲明多種初始化方法提供給子類,子類按照自身需求可以動態的調用父類的方法完成
特定的初始化操作;
例如uiview的實現
initwithframe
init
一個從嚴格意義上講的工廠模式應該是一個純虛的構造方法.父類並不進行初始化,而是有子類進行具體對象的創建
父類的初始化方法可以理解為工廠;
開放不同接口攜帶不同參數的初始化方法可以理解為父類所提供的多個工廠;
子類可以通過其特定的工廠,生產出特定的對象
實際上 很多程序員在開發的時候並沒有工廠模式的這種概念,但是實際上他們在實現程序功能的時候往往會用到工廠
方法的開發思想; 從我的角度出發,我認為工廠方法是一種規范化的編程習慣和規范,也是眾多前輩在開發過程中積累的
經驗,可以提升開發效率和可維護性
IOS的Foundation框架中,中我們常用到一些類頭 在初始化方法或者調用系統方法的時候用到,也有些人管這個叫做類簇
比如說 NSArray NSString NSDictionary ,我們通過他們創建的對象都是其子類的實例化,並非其本身的實例化
設計優勢的體現:
一個工廠滿足所有要求, 不需要創建過多相似的類 簡化了編程,方便程序員開發;因為很多類名不用記了,這也是面向對象
編程的集中體現
通過上面的分析,我們大致可以總結出工廠這種設計模式的應用場景:
(1)當一個類並不知道要創建的具體對象是什么,交由子類處理
(2)當一些類有相似的行為和結構,只是具體實現不同時,可以抽象出工廠
(3)使用者並不在乎具體類型,只在乎接口約定的行為,並且這種行為有個體差異
實例我借鑒了琿少的工程文檔
首先,我們創建一個抽象的工程類,在其中創建一些私有的子類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#import <Foundation/Foundation.h>
//交通工具的枚舉
typedef
enum
{
car,
boat,
airport,
bycicle,
bus,
taxi
}ToolsName;
//代理
@protocol TransPortationDelegate <NSObject>
-(
void
)toHome:(Class)
class
;
@end
//抽象工廠類
@interface TramsPortationFactory : NSObject
+(TramsPortationFactory*)buyTool:(ToolsName)tool;
//共有的方法接口
-(
int
)shouldPayMoney;
-(
void
)run;
@property(nonatomic,strong)id<TransPortationDelegate>delegate;
@end
//具體實現的子類
@interface CarFactory : TramsPortationFactory
@end
@interface BoatFactory : TramsPortationFactory
@end
@interface AirportFactory : TramsPortationFactory
@end
@interface BycicleFactory : TramsPortationFactory
@end
@interface TaxiFactory : TramsPortationFactory
@end
@interface BusFactory : TramsPortationFactory
@end
|
實現文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#import "TramsPortationFactory.h"
@implementation TramsPortationFactory
//實現的創建方法
+(TramsPortationFactory*)buyTool:(ToolsName)tool{
switch
(tool) {
case
car:
return
[[CarFactory alloc]init];
break
;
case
airport:
return
[[AirportFactory alloc]init];
break
;
case
bycicle:
return
[[BycicleFactory alloc]init];
break
;
case
boat:
return
[[BoatFactory alloc]init];
break
;
case
taxi:
return
[[TaxiFactory alloc]init];
break
;
case
bus:
return
[[BusFactory alloc]init];
break
;
default
:
break
;
}
}
-(
int
)shouldPayMoney{
return
0;
}
-(
void
)run{
[self.delegate toHome:[self
class
]];
}
@end
//各自類實現具體的行為
@implementation CarFactory
-(
int
)shouldPayMoney{
return
50;
}
-(
void
)run{
[super run];
NSLog(@
"car to home"
);
}
@end
@implementation AirportFactory
-(
int
)shouldPayMoney{
return
1000;
}
-(
void
)run{
[super run];
NSLog(@
"fly to home"
);
}
@end
@implementation BoatFactory
-(
int
)shouldPayMoney{
return
300;
}
-(
void
)run{
[super run];
NSLog(@
"boat to home"
);
}
@end
@implementation BusFactory
-(
int
)shouldPayMoney{
return
10;
}
-(
void
)run{
[super run];
NSLog(@
"bus to home"
);
}
@end
@implementation BycicleFactory
-(
int
)shouldPayMoney{
return
0;
}
-(
void
)run{
[super run];
NSLog(@
"run to home"
);
}
@end
@implementation TaxiFactory
-(
int
)shouldPayMoney{
return
100;
}
-(
void
)run{
[super run];
NSLog(@
"go to home"
);
}
@end
|
這樣,我們的一個生產工廠就完成了,在外面,我們只需要知道一個類,我們的抽象父類,就可以實現個子類的行為,示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
- (
void
)viewDidLoad {
[super viewDidLoad];
TramsPortationFactory * tool = [TramsPortationFactory buyTool:car];
tool.delegate=self;
[tool run];
NSLog(@
"花了:%d錢"
,[tool shouldPayMoney]);
TramsPortationFactory * tool2 = [TramsPortationFactory buyTool:airport];
tool2.delegate=self;
[tool2 run];
NSLog(@
"花了:%d錢"
,[tool2 shouldPayMoney]);
}
-(
void
)toHome:(Class)
class
{
NSLog(@
"%@"
,NSStringFromClass(
class
));
}
|
可以看到,對於開發者,我們並不知曉CarFactory類的存在,我們只需要通過TramsPortationFactory類,就能夠操作各種交通工具,達到我們的需求。