public class Factory { public static Product createProduct(String type) { if (type == null) { return null; } if (type.equalsIgnoreCase("A")) { return new ConcreteProductA(); } else if (type.equalsIgnoreCase("B")) { return new ConcreteProductB(); } return null; } // 添加测试方法 public static void main(String[] args) { // 创建产品A Product productA = Factory.createProduct("A"); productA.operation(); // 创建产品B Product productB = Factory.createProduct("B"); productB.operation(); } }