代码:
/** * Description:简单工厂模型(汽车工厂) * @author 李XX2019年9月21日 */ public class SimpleFactoryPattern { public static void main(String[] args) { Audi aAudi = new Audi(); aAudi.run(); Auto aAuto = new Auto(); aAuto.run(); System.out.println("-------------"); CarFactory factory = new CarFactory(); Car c1 = factory.createCar("Auto"); c1.run(); Car c2 = factory.createCar("Audi"); c2.run(); } } class CarFactory { public Car createCar(String type) { if("Audi".equalsIgnoreCase(type))return new Audi(); else if("Auto".equalsIgnoreCase(type))return new Auto(); else return null; } } interface Car { public void run(); } class Audi implements Car { public void run() { System.out.println("奥迪车跑~~~~"); } } class Auto implements Car { public void run() { System.out.println("奥拓车跑~~~~"); } }
原文地址:https://www.cnblogs.com/lhh666/p/11562319.html
时间: 2024-10-25 12:25:11