// 产品 Plant接口 public interface Plant { } //具体产品PlantA,PlantB public class PlantA implements Plant {
public PlantA () { System.out.println("create PlantA !"); }
public void doSomething() { System.out.println(" PlantA do something ..."); } } public class PlantB implements Plant { public PlantB () { System.out.println("create PlantB !"); }
public void doSomething() { System.out.println(" PlantB do something ..."); } } // 产品 Fruit接口 public interface Fruit { } //具体产品FruitA,FruitB public class FruitA implements Fruit { public FruitA() { System.out.println("create FruitA !"); } public void doSomething() { System.out.println(" FruitA do something ..."); } } public class FruitB implements Fruit { public FruitB() { System.out.println("create FruitB !"); } public void doSomething() { System.out.println(" FruitB do something ..."); } } // 抽象工厂方法 public interface AbstractFactory { public Plant createPlant(); public Fruit createFruit() ; } //具体工厂方法 public class FactoryA implements AbstractFactory { public Plant createPlant() { return new PlantA(); } public Fruit createFruit() { return new FruitA(); } } public class FactoryB implements AbstractFactory { public Plant createPlant() { return new PlantB(); } public Fruit createFruit() { return new FruitB(); } } 四、工厂方法模式与简单工厂模式