---
title: "创建型模式--简单工厂模式(Simple Factory)"
author: "zhoumin007"
date: "2007-06-17T13:39:00Z"
tags: ["Apple"]
slug: "simple-factory"
draft: false
lastmod: "2007-06-17T13:39:00Z"
id: 96537
url: "https://zhoumin007.iteye.com/blog/96537"
---

1. 工厂模式专门负责将大量有共同接口的类的实例化。工厂模式可以动态决定将哪个类实例化，不必事先知道每次需要实例化哪个类。

工厂模式主要有以下几种形态：

- 简单工厂模式(Simple Factory)：又称静态工厂方法(Static Factory Method)模式。
- 工厂模式(Factory Method)：又称多态性工厂(Ploymorphic Factory)模式或虚拟构造函数(Virtual Constructor)模式。
- 抽象工厂(Abstract Factory)模式:又称工具箱(Toolkit)模式。

本文将简单工厂模式。

**一：引入**

![](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)       public Fruit getEatFruit(String type)  
![](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)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           if (type.equals("apple"))  
![](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=new Apple();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("banana"))  
![](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=new Banana();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("grape"))  
![](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=new Grape();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](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/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//\*此段代码的问题：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)1:如果有新的水果类型需要加入的化必须修改创建Fruit那段代码,在整个以上代码中这段创建Fruit代码那段是可能会变的，根据encapsulate what varies原则，OCP.原则，这段代码需要提出来。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)2:创建Fruit这段代码可能在其他地方也会用到(Client2,Client3...)，所以这种公共的代码需要单独提出来，而不是分散到各个客户类中。  
![](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 FruitFactory ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       public static  Fruit createFruit(String type)  
![](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)           if (type.equals("apple"))  
![](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=new Apple();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("banana"))  
![](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=new Banana();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("grape"))  
![](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=new Grape();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            //增加一种product，只要修改此一处即可，客户代码不用修改  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            else if (type.equals("orange"))  
![](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=new Orange();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           return fruit;             
![](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/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*       public Fruit getEatFruit(String type)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           Fruit fruit=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           if (type.equals("apple"))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)               fruit=new Apple();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("banana"))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)               fruit=new Banana();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           else if (type.equals("grape"))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)               fruit=new Grape();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           }  
![](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 Fruit getEatFruit(String type)  
![](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=FruitFactory.createFruit(type);  
![](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/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

**二:结构**

![](http://p.blog.csdn.net/images/p_blog_csdn_net/zhoumin008/jt-2001-11-14-simplefactorydiagram.gif)

工厂类角色Creator (FruitFactory)：工厂类在客户端的直接调用下创建产品对象。

抽象产品角色Abstract Product (Fruit)：定义简单工厂创建的对象的父类或它们共同拥有的接口。可以是一个类、抽象类或接口。

具体产品角色ConcreteProduct (Apple, Banana)：定义工厂具体加工出的对象。

简单抽象模式的其他形态

- 在有些情况下Simple Factory可以由抽象产品角色扮演，一个抽象产品类同时是子类的工厂
- 三个角色全部合并：

**三:实际应用** 

1. java.text.DateFormat:用此类或其子类格式化日期或时间。

           

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public abstract class DateFormat extends Format ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public final static DateFormat getDateInstance()  
![](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 get(0, DEFAULT, 2, Locale.getDefault());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static DateFormat get(int timeStyle, int dateStyle,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                                  int flags, Locale loc) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if ((flags & 1) != 0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if (timeStyle &lt; 0 || timeStyle &gt; 3) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                throw new IllegalArgumentException("Illegal time style " + timeStyle);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } else ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            timeStyle = -1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if ((flags & 2) != 0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if (dateStyle &lt; 0 || dateStyle &gt; 3) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                throw new IllegalArgumentException("Illegal date style " + dateStyle);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } else ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            dateStyle = -1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return new SimpleDateFormat(timeStyle, dateStyle, loc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } catch (MissingResourceException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return new SimpleDateFormat("M/d/yy h:mm a");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.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)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)说明：getDateInstance()返回一个DateFormat 实例，实际上最终是new SimpleDateFormat(timeStyle, dateStyle, loc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SimpleDateFormat是DateFormat的子类。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)这样加入新的子类eg:OtherDateFormat时对客户代码没有影响。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Factory角色:DateFormat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Abstract Product角色:DateFormat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Concrete Product角色:SimpleDateFormat  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/

 

**四:优缺点**

- 优点：  
  工厂类含有必要的判断逻辑，可以决定在什么时候创建哪一个产品类的实例，客户端可以免除直接创建产品对象的责任，而仅仅"消费"产品。简单工厂模式通过这种做法实现了对责任的分割。
- 缺点：

<!--THE END-->

1. 当