很多人都用过抽象工厂模式,我是个新手,对于模式只是用过些,不敢谈熟悉,然而应句老话:无知才会胆大.也正因为我是初学者的原因吧,所以才有了一些不知道对不对的想法.
cnblogs的文章很多,我也只看了TerryLee 的文章来cnblogs的,对于TerryLee ,我是很感谢的,所学很厂,基本上我想学的都在他上面有所讨论.所以对于文中都有的模式知识,我也就引用了.
TerryLee的抽象工厂模式图:
受petshop的影响,在抽象工厂模式中的抽象层,我也是喜欢Interface这个关键词的,也许我对interface和Abstract区别的理解并不是很彻底,我用模式并不多,因为我开发的和参与的大型项目并不多,也只是在看到的时候google一下他们的区别,下次要是你再问我,我也就只能装傻了.
在WCF中,interface被当作一个协议的契约,就像所有的实际的和非实际的契约一样,我还是喜欢他的功能的单一分化,契约就是契约,它只让告诉你协议格式是怎样的,就像你的信应该怎么写一样,至于你的协议里写的是张三或者李四,要么就是卖车,还是卖房子,这个契约是不用管的.
Interface类是不可直接实类化的(abstract同样).里面的方法是不需要实现,而且必须要在实现接口的类中实现,abstract是在子类中实现,不同的是abstract里的方法不一定都得是虚的,可以定义自己实现好了的方法.因为interface只能定义不能在接口中实现的接口方法,所以对于interface来说变量也是无用的,也就不能定义变量了.
在抽象类工厂中,我们也可以看interface是一种契约,一个产品模型的契约,定义汽车的样式和功能,飞机的样式和功能......
为什么人们给这种方法起名的时间叫他工厂模式呢,那就点有工厂的样式,先给个代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
使用者给工厂一个ICar契约,就是叫工厂做一个什么样子的东西,工厂在符合契约的基础上启到方案A或B,工厂造出了产品给user,user用他来实现他的功能.
也许有些会想,这些只要interface就能实现了,为什么还要factory干什么呢,这就是工厂一个重要的功能了,他的工厂整体的改动,如多语言化,数据库的整体迁移.这就是path的改变,path所代表的命名空间的改变,可以轻松便宜地实现这一计划.