/**
* @author gooing TODO To change the template for this generated type
* comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public abstract class Garden {
public abstract Plant getCenter();
public abstract Plant getBorder();
//Plant.java 實現對花園中植物的基本抽象,此處只提供一個植物的name屬性
package pkgfactory;
public class Plant {
public Plant(String name) {
this.name = name;
}
public String getName() {
return name;
}
private String name;
}
//VerGarden.java 和FlowerGarden.java 分別實現了一個菜園子和花園子
package pkgfactory;
public class VegGarden extends Garden {
public Plant getCenter() {
return new Plant("Wheat");
}
public Plant getBorder() {
return new Plant("Carrot");
}
}
package pkgfactory;
public class FlowerGarden extends Garden {
public Plant getCenter() {
return new Plant("Rose");
}
public Plant getBorder() {
return new Plant("JuHua");
}
}
//Gardener.java是該常式的一個驅動
package pkgfactory;
public class Gardener {
public static void main(String[] args) {
Garden g1 = new FlowerGarden();
Garden g2 = new VegGarden();
g1.memo();
g2.memo();
}
}