摘要:相信大家对于Factory Method设计模式都已经不陌生了,在Factory Method中,对于每一个具体的产品都需要有一个与之对应的工厂类,随着具体的产品越来越多,我们对于工厂类的管理就越来越困难,那如何通过IOC容器来实现对工厂的管理呢?本文将给你答案,通过TypedFactory Facility来实现。
主要内容
1.TypedFactory Facility概述
2.TypedFactory Facility快速入门
一.TypedFactory Facility概述
相信大家对于Factory Method设计模式都已经不陌生了,在Factory Method中,对于每一个具体的产品都需要有一个与之对应的工厂类,随着具体的产品越来越多,我们对于工厂类的管理就越来越困难,那如何通过IOC容器来实现对工厂的管理呢?本文将给你答案。在开始使用之前,我们还是先来看一下Castle官方网站中给出的有关TypedFactory Facility的信息:
Facility Information | |
Uses proxy | Yes |
Requires configuration | Yes |
Uses attributes | No |
Version | Beta 2 |
二.TypedFactory Facility快速入门
1.创建产品接口
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2.定义多个具体的产品,实现于接口Iproduct
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
3.定义工厂接口
在工厂接口中,只有一个方法Create()
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
当然了我们也可以为组件加上一个释放的方法Release()
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4.创建配置文件
在开始的Facility信息中,可以看到TypedFactory Facility是需要配置信息
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5.使用TypedFactory Facility
有了前面几步的准备,现在就可以使用了,只需要注册Facility就可以了,容器会自动根据配置信息来注册工厂类的实现
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://terrylee.cnblogs.com/Images/dot.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
完整的TypedFactory Facility使用过程就是这样的,可以看到,从始至终在程序中只出现了一个工厂类的接口,并没有为每一个具体的产品创建一个工厂类,但是最后我们却可以像使用具体工厂类那样使用工厂的实例。同时,如果我们需要再新增一个新的产品就非常简单了。在这里还需要注意的一点就是在获取工厂的实例时所用到的Key应该是在配置文件中<factory>节点中配置的工厂ID,而不能是Facility的ID!本篇文章就到这里,下一篇我会介绍TypedFactory Facility的实现原理,希望感兴趣的朋友关注!