之前有一篇简单工厂模式的介绍:http://liyf155.iteye.com/blog/1180975,里面介绍了简单工厂模式。简单工厂模式的缺点是很明显的,对“开-闭”原则的支持不够,即扩展性不好,如果有新的产品加入到系统中,那么就要去修改工厂类,并且将必要的逻辑加入到工厂类中,工厂方法模式的引进,既保留了简单工厂模式的有点,又克服了它的缺点。
首先,在工厂方法模式中,核心的工厂类不再负责所有的产品的创建,而是将具体创建的工作交给子类去做。这个核心类则摇身一变成了一个抽象工厂角色,仅负责给出子类需要实现得接口,而不用关心具体的实现细节。
这种进一步抽象化得结果,使这种工厂方法模式可以用来允许系统在不修改具体工厂角色的情况下引进新的产品,这一特点无疑使得工厂方法模式具有超过简单工厂模式的优越性。
下面给出一个例子:、
- package factorymethod;
- // 水果类接口
- public interface FruitFactory
- {
- void grow();
- void havest();
- void plant();
- }
package factorymethod; // 水果类接口 public interface FruitFactory { void grow(); void havest(); void plant(); }
- package factorymethod;
- // 园丁类接口,依赖水果类接口
- public interface FruitGardener
- {
- FruitFactory generate();
- }
package factorymethod;
// 园丁类接口,依赖水果类接口
public interface FruitGardener
{
FruitFactory generate();
}
- package factorymethod;
- public class AppleGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Apple();
- }
- }
package factorymethod;
public class AppleGardener implements FruitGardener
{
public FruitFactory generate()
{
return new Apple();
}
}
- package factorymethod;
- public class GrapeGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Grape();
- }
- }
package factorymethod;
public class GrapeGardener implements FruitGardener
{
public FruitFactory generate()
{
return new Grape();
}
}
- package factorymethod;
- public class OrangeGardener implements FruitGardener
- {
- public FruitFactory generate()
- {
- return new Orange();
- }
- }
package factorymethod;
public class OrangeGardener implements FruitGardener
{
public FruitFactory generate()
{
return new Orange();
}
}
- package factorymethod;
- public class Apple implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Apple growing......");
- }
- public void havest()
- {
- System.out.println("Apple havested......");
- }
- public void plant()
- {
- System.out.println("Apple planted......");
- }
- }
package factorymethod;
public class Apple implements FruitFactory
{
public void grow()
{
System.out.println("Apple growing......");
}
public void havest()
{
System.out.println("Apple havested......");
}
public void plant()
{
System.out.println("Apple planted......");
}
}
- package factorymethod;
- public class Orange implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Orange growing......");
- }
- public void havest()
- {
- System.out.println("Orange havested......");
- }
- public void plant()
- {
- System.out.println("Orange planted......");
- }
- }
package factorymethod;
public class Orange implements FruitFactory
{
public void grow()
{
System.out.println("Orange growing......");
}
public void havest()
{
System.out.println("Orange havested......");
}
public void plant()
{
System.out.println("Orange planted......");
}
}
- package factorymethod;
- public class Grape implements FruitFactory
- {
- public void grow()
- {
- System.out.println("Grape growing......");
- }
- public void havest()
- {
- System.out.println("Grape havested......");
- }
- public void plant()
- {
- System.out.println("Grape planted......");
- }
- }
package factorymethod;
public class Grape implements FruitFactory
{
public void grow()
{
System.out.println("Grape growing......");
}
public void havest()
{
System.out.println("Grape havested......");
}
public void plant()
{
System.out.println("Grape planted......");
}
}
- package factorymethod;
- public class Test
- {
- public static void main(String[] args)
- {
- FruitGardener appleGardener = new AppleGardener();
- FruitGardener orangeGardener = new OrangeGardener();
- FruitGardener grapeGardener = new GrapeGardener();
- FruitFactory fruit = appleGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- System.out.println("***************************");
- fruit = orangeGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- System.out.println("***************************");
- fruit = grapeGardener.generate();
- fruit.plant();
- fruit.grow();
- fruit.havest();
- }
- }
package factorymethod;
public class Test
{
public static void main(String[] args)
{
FruitGardener appleGardener = new AppleGardener();
FruitGardener orangeGardener = new OrangeGardener();
FruitGardener grapeGardener = new GrapeGardener();
FruitFactory fruit = appleGardener.generate();
fruit.plant();
fruit.grow();
fruit.havest();
System.out.println("***************************");
fruit = orangeGardener.generate();
fruit.plant();
fruit.grow();
fruit.havest();
System.out.println("***************************");
fruit = grapeGardener.generate();
fruit.plant();
fruit.grow();
fruit.havest();
}
}
从代码中分析可以看出,FruitGardener这个抽象工厂类依赖FruitFactory这个抽象产品类,这两个类只是定义了需要被子类实现的方法而已,而子类具体要怎么实现可以不管。当系统需要添加一个新的产品,比如说Banana香蕉,那么只要一个BananaGardener实现FruitGardener接口,Banana实现FruitFactory接口,就实现了系统的扩展功能。