---
title: "创建型模式--工厂方法模式(Factory Method)"
author: "zhoumin007"
date: "2007-06-17T19:08:00Z"
tags: ["Apple", "EJB", "出版"]
slug: "factory-method"
draft: false
lastmod: "2007-06-17T19:08:00Z"
id: 96536
url: "https://zhoumin007.iteye.com/blog/96536"
---

**Factory Method:**Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

工厂方法模式又叫虚拟构造子(Virtual Constructor)模式。

**一：引入**

      Simple Factory在一定程度上支持OCP，但并没有完全支持OCP，其一缺点为当有新的产品加入到系统中时还必须修改工厂类。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public interface FruitFactory ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public Fruit createFruit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class AppleFactory implements FruitFactory ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public Fruit createFruit()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return new Apple();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class Client...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       public static  Fruit getEatFruit()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)       ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           Fruit fruit=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           FruitFactory fruitFactory=new AppleFactory();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           fruit=fruitFactory.createFruit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           fruit.pick();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           fruit.peel();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           return fruit;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       public static void main(String args\[])  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)       ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           getEatFruit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//这样有新的产品加入时，只要加入对应的Factory,不用修改Simple Factory 中创建部分的代码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

**二：结构**

![](http://p.blog.csdn.net/images/p_blog_csdn_net/zhoumin008/FactoryMethod.gif)

**三：实际应用**

1. EJB技术构架中

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Context ctx=new InitContext();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)EmployeeHome home=(EmployeeHome )ctx.lookup("Employee");//得到Concrete Factory (EmployeeHome)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Employee emp=home.create(1001,"andy","Smith");//创建Concrete Product(Employee)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)emp.setTel("010-1234343");

 

**四：适用情形**

Use the Factory Method pattern when

- a class can't anticipate the class of objects it must create.
- a class wants its subclasses to specify the objects it creates.
- classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

接口稳定，创建的具体类不可预料，经常变；

当然如果这个类十分稳定，就没有必要用Factory模式，如String类，十分稳定，没有其它的子类。

**参考文献：**  
1:阎宏，《Java与模式》，电子工业出版社  
2:Eric Freeman & Elisabeth Freeman，《Head First Design Pattern》，O'REILLY  
3:GOF ,《designpatterns-elements.of.reuseable.object-oriented.software》