这是一个关于远古时期吃水果的时候,在从前的从前有两种水果,一种叫做香蕉,
public class Banana{
public void beEaten(){"香蕉被吃了"}
}
public class Apple{
public void beEaten(){“苹果被吃了”}
}
吃香蕉就 Banana banana=new Banana();
想吃苹果就 Apple apple=new Apple();
这样直到小地主继位,这个小地主奇懒无比,他就想着好麻烦啊 于是
public class Fruit{
public void beEaten(){“水果被吃了”}
}
public class Banana extends Fruit{
public void beEaten(){"香蕉被吃了"}
}
一种叫做苹果
public class Apple extends Fruit{
public void beEaten(){“苹果被吃了”}
}
他就变成了 想吃香蕉就 Fruit fruit =new Banana();
想吃苹果就 Fruit fruit =new Apple();
后来他连自己去摘水果都不去了,于是他就找了个管家
public class FruitFactory{
public static Fruit CreatFruit(String fruit){
Fruit fruit =null;
switch(fruit){
case "apple":
fruit=new Apple();
break;
case "banana"
fruit =new Banana();
break;
}
return fruit;
}
}
就变成了这样
“管家!苹果!” Fruit fruit =FruitFactory.CreatFruit(“apple”);
“管家!香蕉!” Fruit fruit =FruitFactory.CreatFruit(“banana”);
这就是简单工厂模式了。
简单工厂模式是类的创建模式,又叫做静态工厂方法( Static Factory Method )模式。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
简单工厂模式的优点:
模式的核心是工厂类。这个类含有必要的判断逻辑,可以决定在什么时候创建哪一个产品类的实例。而客户端则可以免除直接创建对象的责任(比如那个小地主)。简单工厂模式通过这种做法实现了对责任的分割。
简单工厂模式的缺点:
这个工厂类集中了所以的创建逻辑,当有复杂的多层次等级结构时,所有的业务逻辑都在这个工厂类中实现。什么时候它不能工作了,整个系统都会受到影响。并且简单工厂模式违背了 开闭原则 (对扩展的开放,对修改的关闭)。
简单工厂模式在java中的应用:
1.DateFormat:jdk中的一个工具类java.text.DateFormat,用来格式化一个本地日期与时间
通过源码我们可以知道DateFormat是一个抽象类(abstract),下面的源代码就是这个类里包含的方法,其实这些就是静态工厂方法,通过静态方法来提供自己的实例是完全可以的(抽象类本身不能进行实例化)。从源码可以看出getDateInstance()方法做了两件事情:
一.运用了多态性:由于SimpleDateFormat是DateFormat的子类,而getDateInstance()声明的类型为DateFormat而实际返回类型为子类SimpleDateFormat
二.使用了静态工厂方法(static):由于DateFormat是抽象类不能进行实例化,因此也就不能调用其中的普通方法(非静态方法)。因此我们必须将其声明为static,才能返回实例
通过上面做的两件事情就将具体子类的实例化过程隐藏起来了,调用者不必考虑具体子类的实例化,因为抽象类会提供它的合适子类实例
源代码:
/**
* Gets the date formatter with the default formatting style
* for the default locale.
* @return a date formatter.
*/
public final static DateFormat getDateInstance()
{
return get(0, DEFAULT, 2, Locale.getDefault());
}
/**
* Gets the date formatter with the given formatting style
* for the default locale.
* @param style the given formatting style. For example,
* SHORT for "M/d/yy" in the US locale.
* @return a date formatter.
*/
public final static DateFormat getDateInstance(int style)
{
return get(0, style, 2, Locale.getDefault());
}
/**
* Gets the date formatter with the given formatting style
* for the given locale.
* @param style the given formatting style. For example,
* SHORT for "M/d/yy" in the US locale.
* @param aLocale the given locale.
* @return a date formatter.
*/
public final static DateFormat getDateInstance(int style,
Locale aLocale)
{
return get(0, style, 2, aLocale);
}
/**
* Creates a DateFormat with the given time and/or date style in the given
* locale.
* @param timeStyle a value from 0 to 3 indicating the time format,
* ignored if flags is 2
* @param dateStyle a value from 0 to 3 indicating the time format,
* ignored if flags is 1
* @param flags either 1 for a time format, 2 for a date format,
* or 3 for a date/time format
* @param loc the locale for the format
*/
private static DateFormat get(int timeStyle, int dateStyle,
int flags, Locale loc) {
if ((flags & 1) != 0) {
if (timeStyle < 0 || timeStyle > 3) {
throw new IllegalArgumentException("Illegal time style " + timeStyle);
}
} else {
timeStyle = -1;
}
if ((flags & 2) != 0) {
if (dateStyle < 0 || dateStyle > 3) {
throw new IllegalArgumentException("Illegal date style " + dateStyle);
}
} else {
dateStyle = -1;
}
try {
// Check whether a provider can provide an implementation that's closer
// to the requested locale than what the Java runtime itself can provide.
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(DateFormatProvider.class);
if (pool.hasProviders()) {
DateFormat providersInstance = pool.getLocalizedObject(
DateFormatGetter.INSTANCE,
loc,
timeStyle,
dateStyle,
flags);
if (providersInstance != null) {
return providersInstance;
}
}
return new SimpleDateFormat(timeStyle, dateStyle, loc);
} catch (MissingResourceException e) {
return new SimpleDateFormat("M/d/yy h:mm a");
}
}
以上那个应用其实是将工厂角色与抽象产品角色进行合并:也就是说一个抽象产品类同时也是子类的工厂类。