public interface Car { String getBrand(); } public class BenzCar implements Car { @Override public String getBrand() { return "奔驰"; } } public class MaybachCar implements Car { @Override public String getBrand() { return "迈巴赫"; } } public interface CarFactory { Car createCar(); } public class BenzCarFactory implements CarFactory { @Override public BenzCar createCar() { returnnew BenzCar(); } } public class MaybachCarFactory implements CarFactory { @Override public MaybachCar createCar() { returnnew MaybachCar(); } } public class FactoryMethodDemo { public static void main(String[] args) { BenzCarFactory benzCarFactory =new BenzCarFactory(); BenzCar benzCar = benzCarFactory.createCar(); System.out.println(benzCar.getBrand()); MaybachCarFactory maybachCarFactory =new MaybachCarFactory(); MaybachCar maybachCar = maybachCarFactory.createCar(); System.out.println(maybachCar.getBrand()); } }