FruitGardener.java: public class FruitGardener public static Fruit factory(String which) throws BadFruitException if (which.equalsIgnoreCase("apple")) return new Apple(); else if (which.equalsIgnoreCase("strawberry")) return new Strawberry(); else if (which.equalsIgnoreCase("grape")) return new Grape(); else throw new BadFruitException("Bad fruit request");
Fruit.java: public interface Fruit void grow(); void harvest(); void plant();
Apple.java: public class Apple implements Fruit private int treeAge; public void grow() //苹果的生长函数代码 public void harvest() //苹果的收获函数代码 public void plant() //苹果的种植函数代码 public int getTreeAge() return treeAge; public void setTreeAge(int treeAge) this.treeAge = treeAge;
Grape.java: public class Grape implements Fruit private boolean seedless;
public void grow() //葡萄的生长函数代码 public void harvest() //葡萄的收获函数代码 public void plant() //葡萄的种植函数代码
public boolean getSeedless() return seedless; public void setSeedless(boolean seedless) this.seedless = seedless;
Strawberry.java: public class Strawberry implements Fruit private boolean coteless; public void grow() //草莓的生长函数代码 public void harvest() //草莓的收获函数代码 public void plant() //草莓的种植函数代码 public boolean getCoteless() return coteless; public void setCoteless(boolean coteless) this. coteless = coteless;
抽象工厂模式: 现在工厂再次大发展,要引进塑料大棚技术,在大棚里种植热带(Tropical)和亚热带的 水果和蔬菜(Veggie)其中水果分为TropicalFruit和NorthernFruit,蔬菜分为TropicalVeggie 和NorthernVeggie,园丁包括TropicalGardener和NorthernGardener,也就是说, TropicalGardener专门管理TropicalFruit和TropicalGardener,NorthernGardener专门 管理NorthernFruit和NorthernVeggie。抽象工厂模式在这个例子中的源码如下所示: Fruit.java: public interface Fruit
NorthernFruit.java: public class NorthernFruit implements Fruit private String name; public NorthernFruit(String name) public String getName() return name; public void setName(String name) this.name = name;
TropicalFruit.java: public class TropicalFruit implements Fruit private String name; public TropicalFruit(String name) public String getName() return name; public void setName(String name) this.name = name;
Veggie.java: public interface Veggie
TropicalVeggie.java: public class TropicalVeggie implements Veggie private String name; public TropicalVeggie(String name) public String getName() return name; public void setName(String name) this.name = name;
NorthernVeggie.java: public class NorthernVeggie implements Veggie private String name; public NorthernVeggie(String name) public String getName() return name; public void setName(String name) this.name = name;
Gardener.java: public interface Gardener Fruit createFruit(String name); Veggie createVeggie(String name);
TropicalGardener.java: public class TropicalGardener implements Gardener public Fruit createFruit(String name) return new TropicalFruit(name); public Veggie createVeggie(String name) return new TropicalVeggie(name);
NorthernGardener.java: public class NorthernGardener implements Gardener public Fruit createFruit(String name) return new NorthernFruit(name); public Veggie createVeggie(String name) return new NorthernVeggie(name);
为了简单起见,这里只讲一下增加新产品(族)时该系统如何扩展, 比如现在要增加南方水果(SouthFruit)和南方蔬菜(SouthVeggie)那 么只要增加如下代码即可很容易的扩展: SouthFruit.java: public class SouthFruit implements Fruit private String name; public SouthFruit (String name) public String getName() return name; public void setName(String name) this.name = name;
SouthVeggie.java: public class SouthVeggie implements Veggie private String name; public SouthVeggie (String name) public String getName() return name; public void setName(String name) this.name = name;
SouthGardener.java: public class SouthGardener implements Gardener public Fruit createFruit(String name) return new SouthFruit(name); public Veggie createVeggie(String name) return new SouthVeggie(name); 在抽象工厂模式中,抽象产品 (AbstractProduct) 可能是一个或多个,从而 构成一个或多个产品族(Product Family)。 在只有一个产品族的情况下,抽象工厂模式实际上退化到工厂方法模式。