《Java与模式》学习笔记——Factory Method
Posted brooksychen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Java与模式》学习笔记——Factory Method相关的知识,希望对你有一定的参考价值。
工厂方法(Factory Method)模式
工厂方法模式又叫虚拟构造子(Virtual Constructor)模式或多态性工厂(Polymorphic Factory)模式。
工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。
工厂方法模式的系统涉及到以下的角色:抽象工厂(Creator)角色、具体工厂角色(Concrete Creator)角色、抽象产品(Product)角色、具体产品(Concrete Product)角色。
结构图如下:(图是从其他地方找来的,所以跟示例代码不完全一样)
![](https://p-blog.csdn.net/images/p_blog_csdn_net/brooksychen/FactoryMethod.jpg)
示例代码:
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/093d4cd127bb4f0b9b7f69cd25677a07.jpg)
![](https://image.cha138.com/20210523/d48f30caf51e4178af59169afff4333f.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/8930496c2be7449387105983ff1273a5.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/093d4cd127bb4f0b9b7f69cd25677a07.jpg)
![](https://image.cha138.com/20210523/d48f30caf51e4178af59169afff4333f.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/8930496c2be7449387105983ff1273a5.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/093d4cd127bb4f0b9b7f69cd25677a07.jpg)
![](https://image.cha138.com/20210523/d48f30caf51e4178af59169afff4333f.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/8930496c2be7449387105983ff1273a5.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/093d4cd127bb4f0b9b7f69cd25677a07.jpg)
![](https://image.cha138.com/20210523/d48f30caf51e4178af59169afff4333f.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/8930496c2be7449387105983ff1273a5.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
![](https://image.cha138.com/20210523/eb47c9b9b39f41d2a66f5937a26578f1.jpg)
![](https://image.cha138.com/20210523/ab2ab6f5cce94875bd210e7730bf78af.jpg)
![](https://image.cha138.com/20210523/67e4053e6154468ebf8b8b5fd219bd7d.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/093d4cd127bb4f0b9b7f69cd25677a07.jpg)
![](https://image.cha138.com/20210523/d48f30caf51e4178af59169afff4333f.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/2f856433a002494db79c7532e8aad114.jpg)
![](https://image.cha138.com/20210523/8930496c2be7449387105983ff1273a5.jpg)
![](https://image.cha138.com/20210523/e0e23b350ffa4e57ba4cbed8dc1dc772.jpg)
工厂方法模式是一个很常见的设计模式,可以在Java语言API的各个角落里面找到。比如java.util.Collection接口,这个接口规定所有的Java集合必须提供一个iterator()方法,返还一个Iterator类型的对象,这个iterator()方法就是一个工厂方法。同样的,List接口给出了两个工厂方法,一个是跟Collection一样的iterator()方法,另一个是listIterator()方法,返还一个ListIterator类型的对象。另外,在微软的COM技术架构中、Sun的EJB技术架构中和JMS技术中,都使用了工厂方法模式。
以上是关于《Java与模式》学习笔记——Factory Method的主要内容,如果未能解决你的问题,请参考以下文章