简单理解:有个工厂对象,工厂对象可以创建多种类型的产品对象。
package Creating; public class FactoryMain { public static void main(String[] args) { ProductA pA = (ProductA) new FactoryA().newProduct(); pA.showProduct(); ProductB pB = (ProductB) new FactoryB().newProduct(); pB.showProduct(); } } interface Factory{ public Product newProduct(); } interface Product{ public void showProduct(); } class ProductA implements Product{ @Override public void showProduct() { System.out.println("生产了A产品!"); } } class ProductB implements Product{ @Override public void showProduct() { System.out.println("生产了B产品!"); } } class FactoryA implements Factory{ @Override public Product newProduct() { return new ProductA(); } } class FactoryB implements Factory{ @Override public Product newProduct() { return new ProductB(); } }