經過上面的分析,咱們大體能夠總結出工廠這種設計模式的應用場景:
程序員
(1)當一個類並不知道要建立的具體對象是什麼,交由子類處理編程
(2)當一些類有類似的行爲和結構,只是具體實現不一樣時,能夠抽象出工廠設計模式
(3)使用者並不在意具體類型,只在意接口約定的行爲,而且這種行爲有個體差別框架
實例我借鑑了琿少的工程文檔iview
首先,咱們建立一個抽象的工程類,在其中建立一些私有的子類:
工具
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
|
實現文件以下:
ui
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
|
這樣,咱們的一個生產工廠就完成了,在外面,咱們只須要知道一個類,咱們的抽象父類,就能夠實現個子類的行爲,示例以下:
atom
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類,就可以操做各類交通工具,達到咱們的需求。spa