public abstract class Computer { public abstract String getRAM(); public abstract String getHDD(); public abstract String getCPU(); @Override public String toString() { return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU(); }}
public class PC extends Computer{ private String ram; private String hdd; private String cpu; public PC() { } public PC(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; }}
public class Server extends Computer { private String ram; private String hdd; private String cpu; public Server() { } public Server(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; }}
public class ComputerFactory { /*使用 字符串 + 分支选择,来创建指定的子对象*/ public static Computer getComputer(String type, String ram, String hdd, String cpu) { if ("PC".equalsIgnoreCase(type)) { return new PC(ram, hdd, cpu); } else if ("Server".equalsIgnoreCase(type)) { return new Server(ram, hdd, cpu); } return null; } /*使用反射调用无参构造函数创建对象*/ public static Computer getComputer(Class<? extends Computer> classType) { System.out.println("classType.getName() = " + classType.getName()); Computer obj = null; try { obj = (Computer) Class.forName(classType.getName()).getDeclaredConstructor().newInstance(); } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException | IllegalAccessException e) { e.printStackTrace(); } return obj; } /*使用反射条用有参构造函数创建对象*/ public static Computer getComputer(Class<? extends Computer> classType, String ram, String hdd, String cpu) { System.out.println("classType.getName() = " + classType.getName()); Computer obj = null; try { Constructor<?> constructor = Class.forName(classType.getName()).getConstructor(String.class, String.class, String.class); obj = (Computer) constructor.newInstance(ram, hdd, cpu); } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException | IllegalAccessException e) { e.printStackTrace(); } return obj; }}
public class Client { /*public static void main(String[] args) { Computer pc = ComputerFactory.getComputer("pc", "2 GB", "500 GB", "2.4 GHz"); Computer server = ComputerFactory.getComputer("server", "16 GB", "1 TB", "2.9 GHz"); System.out.println("Factory PC Config::" + pc); System.out.println("Factory Server Config::" + server); }*/ /*public static void main(String[] args) { Computer computer = ComputerFactory.getComputer(PC.class); System.out.println("computer = " + computer); }*/ public static void main(String[] args) { Computer pc = ComputerFactory.getComputer(PC.class, "2 GB", "500 GB", "2.4 GHz"); Computer server = ComputerFactory.getComputer(Server.class, "16 GB", "1 TB", "2.9 GHz"); System.out.println("Factory PC Config::" + pc); System.out.println("Factory Server Config::" + server); }}