You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
667 B

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();
}
}