《Java与模式》学习笔记——Abstract Factory
Posted brooksychen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Java与模式》学习笔记——Abstract Factory相关的知识,希望对你有一定的参考价值。
抽象工厂(Abstract Factory)模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。
抽象工厂模式与工厂方法模式的最大区别就在于,工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则需要面对多个产品等级结构。
为了方便引进抽象工厂模式,特地引进一个新的概念:产品族(Product Family)。所谓产品族,是指位于不同产品等级结构中,功能相关联的产品组成的家族。
抽象工厂模式的结构图如下:
![](https://image.cha138.com/20210523/74c58c42c7b246c9adef36c1f54776c2.jpg)
源代码:
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/194743011c964b27b3012bfe9bf36e52.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
![](https://image.cha138.com/20210523/e2e0898bf57c417c956758409fe7b2d1.jpg)
![](https://image.cha138.com/20210523/11db71fce1844edab78c27767e58f8fb.jpg)
![](https://image.cha138.com/20210523/9ddd271dfab9401ca10984a0fc59c4bf.jpg)
![](https://image.cha138.com/20210523/3257b331ea844edb882bdabfe34e2888.jpg)
![](https://image.cha138.com/20210523/36269fcd1c944f36a971b6efa3ca7c96.jpg)
![](https://image.cha138.com/20210523/ae63910a96904262934cf4ca33e75b3d.jpg)
![](https://image.cha138.com/20210523/b3d96a43c07b405da37d7b252ce2ff09.jpg)
在以下情况下应当考虑使用抽象工厂模式:
1. 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。
2. 这个系统的产品有多于一个的产品族,而系统只消费其中某一族的产品(这一条是抽象工厂模式的原始用意)。
3. 同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。
4. 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。
当系统中的产品族增加时,抽象工厂模式是支持“开-闭”原则的;但对于产品等级结构的增加,抽象工厂模式不支持“开-闭”原则。
以上是关于《Java与模式》学习笔记——Abstract Factory的主要内容,如果未能解决你的问题,请参考以下文章
《Java与模式》学习笔记——Abstract Factory